05-05-2007, 03:39
|
|
|
חבר מתאריך: 15.04.06
הודעות: 377
|
|
בעיות עם קוד עם ה php וה pagination, פרטים בתוכן:
בעיה ראשונה, קטע קוד המיועד להפעלה, אך ורק כאשר לוחצים על התגית a הנבחרת.
אך מופעל בעת הכניסה הראשונה לעמוד עקב כניסה לתנאי. כיצד ניתן לשנות את הקוד הנ"ל שיעבוד אך ורק כאשר לוחצים על אותה תגית a?
הקוד:
קוד PHP:
$next_page=$page+1; if($next_page<=$page_n) { $end = $start + 20; $start= $start + 20; result($start, $end ,$counter ,$page ,$page_n); echo("<b>>>></b><a href=http://localhost/canadachat/users_page_test2.php?cat=$end&$limit=$limit&$page=$next_page<b>next.</b></a>"); }//next_page
תמונה של הפלט:
הפלט: next page: 2 page: 1 הוסר בקוד שהעקתי להודעה, כי הקוד היה מיועד לבדיקה בלבד.
ישנן שני תגיות a נוספות שאמורות לעבוד באותה שיטה, כמו התגית a האחרונה.
בעיה שנייה: בעיה של הצגה כפולה של רשומות, כפי שניתן לראות בתמונה של הפלט בסעיף א ובתמונה הבאה:
שימו לב למספרי הרשומות בתמונה הראשונה, הם נגמרים ב 20 ואז כאשר יש קריאה חוזרת לאותה פונקציה כפי שניתן לראות בתמונה ה 2 רואים שהרשומות מתחילות מ 20 עד 39 וזה לא תקין.
באופן תקין זה אמור להיות מ 1-20 ל 21-40 ל 41-60....
למה ומדוע?
ניסיתי להתחיל לא מאפס אלא מאחד.
הגדרתי את $start שיתחיל מ 1 אך אז הרשומה הראשונה לא מוצגת אז זה לא הפיתרון.
הקודים עם הבעיה: (הפונקציה שמדפיסה על המסך את תוכן המסד.)
קוד PHP:
function result($start, $end ,$counter ,$page ,$page_n) { require($_SERVER['DOCUMENT_ROOT'].'/canadachat/dbconnect.php'); $limit = 20; $result = mysql_query('SELECT * FROM users LIMIT ' . $start . ', ' . $end . ';'); if ( mysql_num_rows($result)==0) { echo "thers aren't any Users in the database. "; } else { for (;$row = mysql_fetch_array($result);) { $id = $row['ID']; $users = $row['nickname']; $gender = $row['Gender']; $country = $row['country']; $age = $row['age']; ?> <table width="600" align="center"> <tr> <td width="120"> <?php echo($id); ?> </td> <td> <?php echo($users); ?> </td> <td width="120"> <?php echo($gender); ?> </td> <td width="120"> <?php echo($country); ?> </td> <td width="120"> <?php if($age!=NULL) { echo($age); } else { echo("age=null"); } ?> </td> </tr> </table> <?php } } echo "there is: $page_n pages.", "<br>"; echo "there are $counter registered users to our website", "<br>"; mysql_close(); }
הקריאות לאותה פונקציה:
קוד PHP:
<?php $counter = func_counter($counter); $start=0; $end = 20; $page_n = pages($counter); $page = first_page ($page_n); result($start, $end ,$counter ,$page ,$page_n); $prev_page=$page-1; if($prev_page>=1) { echo("<b><<<</b><a href=http://localhost/canadachat/users_page_test2.php?cat=$end&$limit=$limit&$page=$prev_page<b>prev.</b></a><br>"); }// prev_page for($a=1;$a<=$page_n;$a++) { if($a==$page){ echo("<b>$a</b>|"); } else{ echo(" <a href=http://localhost/canadachat/users_page_test2.php?cat=limit=$limit&page=$a> $a </a>|"); } }// between pages $next_page=$page+1; if($next_page<=$page_n) {
$end = $start + 20; $start= $start + 20; result($start, $end ,$counter ,$page ,$page_n); echo("<b>>>></b><a href=http://localhost/canadachat/users_page_test2.php?cat=$end&$limit=$limit&$page=$next_page<b>next.</b></a>"); }//next_page ?>
כל הקוד של ה php יחדיו:
קוד PHP:
<div id="inside_div"> <?php function func_counter() { require($_SERVER['DOCUMENT_ROOT'].'/canadachat/dbconnect.php'); $query = mysql_query ("SELECT * FROM users"); $counter=mysql_num_rows($query); return $counter; } function pages($counter) { $page_n = $counter/20; $page_n=intval($page_n)+1; return $page_n; } function first_page ($page_n) { if($page_n!=0) { $page=1; } return $page; } function result($start, $end ,$counter ,$page ,$page_n) { require($_SERVER['DOCUMENT_ROOT'].'/canadachat/dbconnect.php'); $limit = 20; $result = mysql_query('SELECT * FROM users LIMIT ' . $start . ', ' . $end . ';'); if ( mysql_num_rows($result)==0) { echo "thers aren't any Users in the database. "; } else { for (;$row = mysql_fetch_array($result);) { $id = $row['ID']; $users = $row['nickname']; $gender = $row['Gender']; $country = $row['country']; $age = $row['age']; ?> <table width="600" align="center"> <tr> <td width="120"> <?php echo($id); ?> </td> <td> <?php echo($users); ?> </td> <td width="120"> <?php echo($gender); ?> </td> <td width="120"> <?php echo($country); ?> </td> <td width="120"> <?php if($age!=NULL) { echo($age); } else { echo("age=null"); } ?> </td> </tr> </table> <?php } } echo "there is: $page_n pages.", "<br>"; echo "there are $counter registered users to our website", "<br>"; mysql_close(); } ?> <br /> <table width="600" align="center"> <tr> <td width="120">ID:</td> <td width="120">nickname:</td> <td width="120">gender:</td> <td width="120">country:</td> <td width="120">age:</td> </tr> </table> <?php $counter = func_counter($counter); $start=0; $end = 20; $page_n = pages($counter); $page = first_page ($page_n); result($start, $end ,$counter ,$page ,$page_n); $prev_page=$page-1; if($prev_page>=1) { echo("<b><<<</b><a href=http://localhost/canadachat/users_page_test2.php?cat=$end&$limit=$limit&$page=$prev_page<b>prev.</b></a><br>"); }// prev_page for($a=1;$a<=$page_n;$a++) { if($a==$page){ echo("<b>$a</b>|"); } else{ echo(" <a href=http://localhost/canadachat/users_page_test2.php?cat=limit=$limit&page=$a> $a </a>|"); } }// between pages $next_page=$page+1; if($next_page<=$page_n) { $end = $start + 20; $start= $start + 20; result($start, $end ,$counter ,$page ,$page_n); echo("<b>>>></b><a href=http://localhost/canadachat/users_page_test2.php?cat=$end&$limit=$limit&$page=$next_page<b>next.</b></a>"); }//next_page ?> </div>
_____________________________________
חתימתכם הוסרה כיוון שלא עמדה בחוקי האתר. לפרטים נוספים לחצו כאן. תוכלו לקבל עזרה להתאמת החתימה לחוקים בפורום חתימות וצלמיות.
|