Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/129896-yet-another-while-loop-thread/
Share on other sites

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

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

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

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.

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

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.