Eduardop911 Posted December 19, 2012 Share Posted December 19, 2012 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 | Quote Link to comment https://forums.phpfreaks.com/topic/272198-php-looping-not-working/ Share on other sites More sharing options...
twistedvengeance Posted December 20, 2012 Share Posted December 20, 2012 You are calling if(array_search($id, $ids) == true){ Which isn't true. You are defining $ids = $a['ids']; $ids = explode(",", $ids); So just set it to $id = $a['ids']; $ids = explode(",", $id); Quote Link to comment https://forums.phpfreaks.com/topic/272198-php-looping-not-working/#findComment-1400533 Share on other sites More sharing options...
Christian F. Posted December 20, 2012 Share Posted December 20, 2012 (edited) 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 December 20, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/272198-php-looping-not-working/#findComment-1400564 Share on other sites More sharing options...
Eduardop911 Posted December 20, 2012 Author Share Posted December 20, 2012 Thank you guys i will look over it and will get back with updates will start changing the variables as well, since I'm doing this only to get a quick done with the script. Quote Link to comment https://forums.phpfreaks.com/topic/272198-php-looping-not-working/#findComment-1400673 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.