Jump to content

Why is this not looping?


Go to solution Solved by CroNiX,

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/293824-why-is-this-not-looping/
Share on other sites

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. 

  • Solution

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'>";
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.