piearcy Posted January 10, 2015 Share Posted January 10, 2015 How come this doesn't loop? Everything is in the while loop. My database connection is in the included file you see to begin the code which is what db_conx refers to just to be clear. Database connection is not an issue nor is getting values. I just get the first one and nothing more. No looping taking place here. What I miss? require('includes/db_connect.php'); $query = "SELECT * FROM events ORDER BY displayorder ASC"; $result = mysqli_query($db_conx, $query); while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { $pid = $row["id"]; $title = $row["title"]; $date = $row["date"]; $info = $row["info"]; $linkTxt = $row["linkTxt"]; $link = $row["link"]; $message = "<div id='events_holder'><table width='500' border='0' cellspacing='0' cellpadding='10'> <form action='edit_event_parse.php' method='post'> <tr> <td class='prayer_title'>Title:</td> <td><input type='text' name='title' class='admin_input' value='" .$title."' /></td> </tr> <tr> <td class='prayer_title'>Date:</td> <td><input type='text' name='date' class='admin_input' value='".$date."' /></td> </tr> <tr> <td class='prayer_title'>Link Text:</td> <td><input type='text' name='linkTxt' class='admin_input' value='".$linkTxt."' /></td> </tr> <tr> <td class='prayer_title'>Link URL:</td> <td><input type='text' name='link' class='admin_input' value='".$link."' /></td> </tr> <tr> <td class='prayer_title'>Event Details:</td> <td><textarea name='info' cols='20' rows='10' class='admin_area'>".$info."</textarea></td> </tr> <tr> <td><input type='hidden' name='pid' value='".$pid."' /></td> <td><input name='submit' type='submit' value='Edit Event' class='admin_submit'/></td> </tr> </form> </table> <p> </p> <hr /> <p> </p> </div>"; } Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/293824-why-is-this-not-looping/ Share on other sites More sharing options...
requinix Posted January 11, 2015 Share Posted January 11, 2015 You're overwriting $message every time you go through the loop. Either keep building upon $message (such as $message .=) or use it immediately. Quote Link to comment https://forums.phpfreaks.com/topic/293824-why-is-this-not-looping/#findComment-1502470 Share on other sites More sharing options...
ginerjm Posted January 11, 2015 Share Posted January 11, 2015 That - and you have your table/form jumbled up. One has to be completely inside the other. Quote Link to comment https://forums.phpfreaks.com/topic/293824-why-is-this-not-looping/#findComment-1502473 Share on other sites More sharing options...
piearcy Posted January 11, 2015 Author Share Posted January 11, 2015 Indeed on both counts. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/293824-why-is-this-not-looping/#findComment-1502486 Share on other sites More sharing options...
ginerjm Posted January 11, 2015 Share Posted January 11, 2015 HTH! And BTW - your code was actually looping. You just couldn't tell because of your one mistake. Quote Link to comment https://forums.phpfreaks.com/topic/293824-why-is-this-not-looping/#findComment-1502520 Share on other sites More sharing options...
piearcy Posted January 12, 2015 Author Share Posted January 12, 2015 Gotcha. But even changing it to this: require('includes/db_connect.php'); $query = "SELECT * FROM events ORDER BY displayorder ASC"; $result = mysqli_query($db_conx, $query); while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { $pid = $row["id"]; $title = $row["title"]; $date = $row["date"]; $info = $row["info"]; $linkTxt = $row["linkTxt"]; $link = $row["link"]; $message = "<div id='events_holder'>"; $message .= "<table width='500' border='0' cellspacing='0' cellpadding='10'>"; $message .= "<form action='edit_event_parse.php' method='post'>"; $message .= "<tr>"; $message .= "<td class='prayer_title'>Title:</td>"; $message .= "<td><input type='text' name='title' class='admin_input' value='" .$title."' /></td>"; $message .= "</tr>"; $message .= "<tr>"; $message .= "<td class='prayer_title'>Date:</td>"; $message .= "<td><input type='text' name='date' class='admin_input' value='".$date."' /></td>"; $message .= "</tr>"; $message .= "<tr>"; $message .= "<td class='prayer_title'>Link Text:</td>"; $message .= "<td><input type='text' name='linkTxt' class='admin_input' value='".$linkTxt."' /></td>"; $message .= "</tr>"; $message .= "<tr>"; $message .= "<td class='prayer_title'>Link URL:</td>"; $message .= "<td><input type='text' name='link' class='admin_input' value='".$link."' /></td>"; $message .= "</tr>"; $message .= "<tr>"; $message .= "<td class='prayer_title'>Event Details:</td>"; $message .= "<td><textarea name='info' cols='20' rows='10' class='admin_area'>".$info."</textarea></td>"; $message .= "</tr>"; $message .= "<tr>"; $message .= "<td><input type='hidden' name='pid' value='".$pid."' /></td>"; $message .= "<td><input name='submit' type='submit' value='Edit Event' class='admin_submit'/></td>"; $message .= "</tr>"; $message .= "</form>"; $message .= "</table>"; $message .= "<p> </p>"; $message .= "<hr />"; $message .= "<p> </p>"; $message .= "</div>"; I still only get the last message that overwrote everything else. How can I display all the records in this format without overwriting $message each time? This was easy using mysql. Much more difficult it seems using mysqli now. Still learning. Quote Link to comment https://forums.phpfreaks.com/topic/293824-why-is-this-not-looping/#findComment-1502682 Share on other sites More sharing options...
Solution CroNiX Posted January 12, 2015 Solution Share Posted January 12, 2015 It's the same problem as the original. In your loop you have this line: $message = "<div id='events_holder'>"; Each time it cycles through the loop, it overwrites the previous value of $message, because you are setting it using only = You're basically doing this: $name = 'Fred'; $name = 'Bill'; $name = 'Janet'; echo $name; //outputs Janet put a $message = ''; just BEFORE the start of your loop, and change the line above in your loop to be a string concatenation instead of a = $message .= "<div id='events_holder'>"; Quote Link to comment https://forums.phpfreaks.com/topic/293824-why-is-this-not-looping/#findComment-1502684 Share on other sites More sharing options...
piearcy Posted January 12, 2015 Author Share Posted January 12, 2015 Oops. Missed that first one. That did the trick. Thanks so much. Quote Link to comment https://forums.phpfreaks.com/topic/293824-why-is-this-not-looping/#findComment-1502685 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.