Lodius2000 Posted October 24, 2008 Share Posted October 24, 2008 need to figure this one out, and its my first real while loop, also I am not opposed to changing the kind of loop, but this is the only way i could think of to do this while only making one query quick note: the getAll class of $db works similar to mysql_getall(), where it returns an array of the queried columns that match the where clause and the array is separated by each row in the db (perfect for a foreach <?php //$row['imgorder'] contains the string 1,2,5,3,4,6,7,8 and is from an earlier query //get the relevant image data $photos = explode(",", $row['imgorder']); $q = $db->getAll('SELECT image_url, credit, image_caption, id FROM image WHERE article_id = ?', array($id)); //print out the images while ($photos = $q['id']){ print '<div class="imgbox">' . $photo['image_url']; print '<div class="credit">' . $photo['credit'] . "</div>"; print "\n"; print '<div class="caption">' . $photo['image_caption'] . "</div></div>"; print "<br />\n"; } ?> so anyways, this does not work help, please Quote Link to comment https://forums.phpfreaks.com/topic/129896-yet-another-while-loop-thread/ Share on other sites More sharing options...
DarkWater Posted October 24, 2008 Share Posted October 24, 2008 Use a foreach, like you said in your "Quick Note:". =P Quote Link to comment https://forums.phpfreaks.com/topic/129896-yet-another-while-loop-thread/#findComment-673406 Share on other sites More sharing options...
Lodius2000 Posted October 24, 2008 Author Share Posted October 24, 2008 im worried about lag time though because each iteration of the loop would contain a query Quote Link to comment https://forums.phpfreaks.com/topic/129896-yet-another-while-loop-thread/#findComment-673408 Share on other sites More sharing options...
DarkWater Posted October 24, 2008 Share Posted October 24, 2008 I don't quite understand what you mean; I see no second query. Quote Link to comment https://forums.phpfreaks.com/topic/129896-yet-another-while-loop-thread/#findComment-673413 Share on other sites More sharing options...
jwilliam Posted October 24, 2008 Share Posted October 24, 2008 I'm a little confused by your code. As far as I know there is no mysql_getall() function. Just make your query: $sql = "SELECT whatever FROM whatever WHERE blahblahblah;"; $result = mysql_query($sql, $link); while($row = mysql_fetch_assoc($result)) { // print whatever... } That's a pretty standard way of querying your database Quote Link to comment https://forums.phpfreaks.com/topic/129896-yet-another-while-loop-thread/#findComment-673427 Share on other sites More sharing options...
Lodius2000 Posted October 24, 2008 Author Share Posted October 24, 2008 Darkwater now im confused ok take it into the context of this forum, but imagine you had the power to rearrange the order of the comments of this thread and the new order would be stored in $row['imgorder'] then run it through explode so you have an array then you would need to redisplay the comments in their correct new order so you have a query that says (very simply) $q = $db->getAll('SELECT (user who made the comment), comment, (unique id of comments) FROM image WHERE thread number = ?', array($id)); then now you have the unique id of the comments as an element of an array that is unordered because a db doesnt order and the array of the order so you need to make a loop that matches the order to the unique id and then uses the other parts of the query that are in the same row of the array containing that specific unique id the reason I dont want to do a foreach is because it would look like this <?php $photos = explode(",", $row['imgorder']); foreach ($photos as $photo){ $q = $db->getAll('SELECT image_url, credit, image_caption, FROM image WHERE id = ?', array($photo)); print '<div class="imgbox">' . $photo['image_url']; print '<div class="credit">' . $photo['credit'] . "</div>"; print "\n"; print '<div class="caption">' . $photo['image_caption'] . "</div></div>"; print "<br />\n"; } ?> problem with that is 1 query per photo, what if there are 100 photos, thats a lot of server requests, which makes for a very slow load time dont know if that is clearer but its out there Quote Link to comment https://forums.phpfreaks.com/topic/129896-yet-another-while-loop-thread/#findComment-673439 Share on other sites More sharing options...
Lodius2000 Posted October 24, 2008 Author Share Posted October 24, 2008 j william, the problem is that i have to narrow db results by 2 things 1 is static (article_id) and the other is an array (id) so i want to match id to $photos in a loop so that the rest of the database row that that specific id was on Quote Link to comment https://forums.phpfreaks.com/topic/129896-yet-another-while-loop-thread/#findComment-673443 Share on other sites More sharing options...
DarkWater Posted October 24, 2008 Share Posted October 24, 2008 How about: foreach ($db->getAll('SELECT image_url, credit, image_caption FROM image WHERE id IN ({$row['imgorder']})') as $row) { //process $row } Since $row['imgorder'] seems to be a comma separated list, you can just use it in an IN clause. Quote Link to comment https://forums.phpfreaks.com/topic/129896-yet-another-while-loop-thread/#findComment-673444 Share on other sites More sharing options...
Lodius2000 Posted October 24, 2008 Author Share Posted October 24, 2008 darkwater <?php $photos = $row['imgorder']; foreach ($db->getAll('SELECT image_url, credit, image_caption FROM image WHERE id IN $photos') as $photo) { //process $row print '<div class="imgbox">' . $photo['image_url']; print '<div class="credit">' . $photo['credit'] . "</div>"; print "\n"; print '<div class="caption">' . $photo['image_caption'] . "</div></div>"; print "<br />\n"; } ?> prints DB Error: syntax error and i am unfamiliar with IN so i dunno whats wrong Quote Link to comment https://forums.phpfreaks.com/topic/129896-yet-another-while-loop-thread/#findComment-673447 Share on other sites More sharing options...
DarkWater Posted October 24, 2008 Share Posted October 24, 2008 Missed ( ) around $photos. Quote Link to comment https://forums.phpfreaks.com/topic/129896-yet-another-while-loop-thread/#findComment-673508 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.