בעיה מעצבנת...

רוןמ

New member
בעיה מעצבנת...

אני מנסה לשלוף נתונים מטבלה ולהציג אותם בדרך הבאה:
while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td><input type=\"radio\" name=\"AnswerID\" value=\"{$row['AnswerID']}\"></td>"; echo "<td>{$row['Answer']}</td>"; echo "</tr>"; }​
הבעיה היחידה היא שמשום מה זה לא מודפס, ואני גם לא מקבל שגיאה. חשבתי שאולי אין רשומות בטבלה, אבל יש, וגם בדקתי את השאילתה ואת שמות השדות כמה פעמים והכל בסדר. למה הלולאה לא מתבצעת? למישהו יש מושג? תודה מראש, רון.
 
האם רק ערכי $row לא מודפסים או שמא

גם כל מה שאמור להיות מודפס בלולאה, קרי השורות והתאים של הטבלה? אם כל הלולאה לא מוציאה פלט, ואפילו התאים לא מודפסים, תצטרך להוסיף את הקוד שלפני השליפה והכניסה ללולאה עצמה
 

רוןמ

New member
הממ, זה ארוך, אבל הנה:

זה אכן לא מציג שום דבר ממה שיש בלולאה...
<?php // MySQL connection $link = mysql_connect(); if(!$link) die("Couldn't connect to MySQL"); mysql_select_db("southp",$link) or die("Couldn't open \"southp\": " .mysql_error()); $result = mysql_query("SELECT poll_questions.ID as QuestionID, poll_questions.Question as Question, poll_answers.ID as AnswerID, poll_answers.Answer as Answer FROM poll_questions, poll_answers WHERE poll_questions.ID = poll_answers.Question_ID AND poll_questions.Status=1"); while($recordset = mysql_fetch_array($result)) { $Question = $recordset['Question']; $QuestionID = $recordset['QuestionID']; } ?> <form name="Voting" method="post" action="PollVote.php"> <table border="0" cellspacing="0" cellpadding="0" width="260"> <tr> <td colspan="2" align="center"> <?=$Question\n?> <input type="hidden" name="QuestionID" value="<?=$QuestionID?>"> </td> </tr> <?php while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td><input type=\"radio\" name=\"AnswerID\" value=\"{$row['AnswerID']}\"></td>"; echo "<td>{$row['Answer']}</td>"; echo "</tr>"; } ?> <tr> <td colspan="2" align="center"><br> <input type="submit" name="submit" value="ùìç" class="button"> </td> </tr> </table> </form> <?php // Close connection mysql_close($link); ?>​
 
אני חושב שהבנתי את הבעיה

בתחילה את שולף את כל הנתונים ע"י לולאת while לתוך משתנה recordset. אחרי שסיימת לשלוף את הנתונים, ככל הידוע לי, המשתנה result סיים את תפקידו, ואין לו מה לעשות יותר. הנה הצעת התיקון שלי:
<?php // MySQL connection $link = mysql_connect(); if(!$link) die("Couldn't connect to MySQL"); mysql_select_db("southp",$link) or die("Couldn't open \"southp\": " .mysql_error()); $result = mysql_query("SELECT poll_questions.ID as QuestionID, poll_questions.Question as Question, poll_answers.ID as AnswerID, poll_answers.Answer as Answer FROM poll_questions, poll_answers WHERE poll_questions.ID = poll_answers.Question_ID AND poll_questions.Status=1"); $i = 0; while($recordset = mysql_fetch_array($result)) { $Question = $recordset['Question']; $QuestionID = $recordset['QuestionID']; $array1[$i][0] = $recordset['AnswerID']; $array1[$i][1] = $recordset['Answer']; } ?> <form name="Voting" method="post" action="PollVote.php"> <table border="0" cellspacing="0" cellpadding="0" width="260"> <tr> <td colspan="2" align="center"> <?=$Question\n?> <input type="hidden" name="QuestionID" value="<?=$QuestionID?>"> </td> </tr> <?php for($i = 0; $i < count($array1); $i++) { echo "<tr>"; echo "<td><input type=\"radio\" name=\"AnswerID\" value=\"{$array[$i][0]}\"></td>"; echo "<td>{$array[$i][1]}</td>"; echo "</tr>"; } ?> <tr> <td colspan="2" align="center"><br> <input type="submit" name="submit" value="ùìç" class="button"> </td> </tr> </table> </form> <?php // Close connection mysql_close($link); ?>​
והנה גם הסבר: יצרתי מערך ששומר את הנתונים הנשלפים בלולאת ה-while. אחר כך, אי אפשר לנסות לשלופם מחדש בלולאת while דומה, משום שהנתונים כבר נשלפו. לכן במקום השתמשתי ב-for שמשתמש בנתונים שאוחסנו במערך כדי שלא יאבדו. מקווה שזה יעבוד
 
תיקונים

בתוך לולאת ה-while, לאחר השורה הרביעית, צריך להוסיף שורה חמישית והיא
$i++;​
אם count לא יעבוד, אפשר להוסיף את הקוד הבא אחרי לולאת ה-while:
$num = $i;​
ובלולאת ה-for לשנות את התנאי לזה:
$i <= $num​
 

רוןמ

New member
תודה רבה על העזרה ../images/Emo13.gif

הסתדר ככה...
 
למעלה