Jump to content

Php Looping Not Working


Eduardop911

Recommended Posts

hello i did a php message system where it get data from sql and it extracts the message that the person wrote you, but is separated into conversations so in the inbox i want it to show only the last message shown, witch it works ONLY for the first person that sends you a message not for the other persons that send you messages so i made a data base where conversation_hash is unique and you it has ids in string separated by a ","

 

 

<?php
$b = mysql_query("SELECT * FROM conversations");
while($a = mysql_fetch_array($B)){
$hash = $a['conversation_hash'];
$ids = $a['ids'];
$ids = explode(",", $ids);
if(array_search($id, $ids) == true){
$ids = array_filter($ids, 'strlen');
foreach($ids as $o){
if($o !== $id){
print_r($ids);
echo $o;
$y = mysql_query("SELECT * FROM messages WHERE conversation_hash='$hash' AND from_id='$o' ORDER BY message_id DESC ");
$t = mysql_fetch_array($y);
?>
<div class="message">
<center>
<table width="600">
<tr>
<td width="75" rowspan="2">
<img src="/elitequorum/<?php echo $t['image_location']; ?>" height="75" width="75" />
</td>
<td height="39">
</td>
<td height="" rowspan="2">
<table height="75">
<tr>
<td width="450" style="vertical-align:middle;" height="55">
<?php echo $t['message']; ?>
</td>
</tr>
<tr>
<td style="text-align:right; vertical-align:bottom;" height="5">
<?php echo $t['date']; ?>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="75" height="34">
<?php echo $t['username']; ?>
</td>
</tr>
</table>
</center>
</div>
<?php

}
}
}
}

?>

to do some testing if the information is getting where is suppost to i did some print_r(); and the information does go but it doesnt go in the SQL as a loop, i dont get any PHP/SQL error but the script is not working correctly, this is what i get:

----------------------------------------------------------------------

image1| Message1 | date2 |

-----------------------------------------

broken(image2)| empty(message2) | empty(date2) |

----------------------------------------------------------------------------------------

And this is what im suppos to get:

----------------------------------------------------------------------

image1| Message1 | date2 |

-----------------------------------------

image2| message2 | date2 |

Link to comment
Share on other sites

Unfortunately, that doesn't make any sense, twistedvengeance. What you'd end up with there, is searching the array for the comma-separated string you just made into said array. Naturally enough, that won't produce any results.

The $id variable is coming from somewhere outside of the code we see, and that's probably what lead to your confusion. Can't say I blame you though, as I don't really know what it's for either.

 

Eduardop911: What you need to do is to give your variables some meaningful names, $a, $b and so forth doesn't give any insight as to what the variables contain (and thus neither what they're used for). Comments describing your intent with the code would also be nice, as it'll help both other people and yourself, when looking at your code.

Most of all, however, you need to keep in mind that variable names in PHP are case sensitive. $b is not the same as $B. ;)

 

Edit, added: Almost forgot, but you need to look into JOINs as well. You should never run a query inside a loop, as that absolutely kills performance. Instead you should do one query with a JOIN, and then loop over its results.

Edited by Christian F.
Link to comment
Share on other sites

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.