0

I am trying to pull the image link for 10 different rows based on ids in an array. It seems to break when the query is in the loop..Any ideas how i can accomplish this?

$array = array(54, 319, 342, 298, 281, 190,178,158,138,7);


$shuffleKeys = array_keys($array);
shuffle($shuffleKeys);
$newArray = array();
foreach($shuffleKeys as $key) {
    $newArray[$key] = $array[$key];
}


for($i=0;$i<=count($newArray);$i++){

    $query = "SELECT logoName,logoImageLink, logoImageLink2, countryImg, logoArtist, afterText, country FROM logos WHERE id = $array($i)     ";
    $result = mysql_query($query);


    /* fetch rows in reverse order */
    for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) {
    if (!mysql_data_seek($result, $i)) {
        echo "Cannot seek to row $i: " . mysql_error() . "\n";
        continue;
    }

    if (!($row = mysql_fetch_assoc($result))) {
        continue;
    }

    $imageLink = $row['logoImageLink'];

    echo "<li class=\".$array($i).\" ><img src=\".$imageLink.\" /></li>";
}
1

1 Answer 1

2

You can use the MySQL IN clause and do this in a single select.

$ids = join(',',$newArray);  
$query = "SELECT logoName,logoImageLink, logoImageLink2, countryImg, logoArtist, afterText, country FROM logos WHERE id IN ($ids)";
$result = $mysqli->query($query);

while ($row = $result->fetch_assoc()) {
     $imageLink = $row['logoImageLink'];

     echo "<li><img src=\"$imageLink\"/></li>";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is this then like creating a multli level array? Or how do I get the logoImageLink for id 342 then 190 etc?
okay..i have seen this around a little bit. But i'm still a bit confused on how it works... so if i want to say just print the logoImageLink for each id in the array, how can i do that with whats given? If I can see that I think i got it. thx for your help.
$row is an array containing all the returned fields, you can use it the exact way you were in your question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.