azukah Posted March 27, 2012 Share Posted March 27, 2012 i have a slider that shows 2 images at a time. i did a query and got all my images names from the database to be in an array so i can go form index zero to the last image incrementing by +1 but not working. what it is doing now is displaying index[0] and index[1] in the first set of 2 images in the second set is displaying index[1] and index [2] in the third set is displaying index[2] and index[3] and so on... here's my query require_once('connections.php'); mysql_select_db($dbname, $db); $sql = "SELECT * FROM table WHERE image IS NOT NULL LIMIT 6"; //limiting to six for testing only, will remove afterwords $results = mysql_query($sql, $db) or die(mysql_error()); $row = mysql_fetch_assoc($results); // $result2 = mysql_query("SELECT * FROM table WHERE image IS NOT NULL LIMIT 6"); $storeArray = Array(); while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) { $storeArray[] = $row2['image']; } ?> here's the html/php <?php $index = 0;?> <?php do { ?> <div class="slide"> <div class="slidePromo"> <div class="promoImg"><a href="#"><img src="<?php echo $storeArray[$index];?>"/></a> </div> </div> <!--end slide--> <div class="slidePromo"> <div class="promoImg"><a href="#l"><img src="<?php echo $storeArray[++$index];?>"/></a></div> </div> </div> <!--end slide--> <?php } while ($row = mysql_fetch_assoc($results)); ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 27, 2012 Share Posted March 27, 2012 For each result, you are outputting that result and the next one. You are also being redundant with the do/while loop, you end up selecting the data multiple times. You will want to get all of the results into an array *(you've already done that) and then loop through it (using for), that way you can increment the iterator manually. You could also use foreach but it might be trickier given what you're trying to do. Quote Link to comment Share on other sites More sharing options...
cpd Posted March 27, 2012 Share Posted March 27, 2012 require_once('connections.php'); mysql_select_db($dbname, $db); $result2 = mysql_query("SELECT * FROM table WHERE image IS NOT NULL LIMIT 6"); $storeArray = Array(); while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) { ?> <div class="slide"> <div class="slidePromo"> <div class="promoImg"><a href="#"><img src="<?php echo $row2['image'];?>"/></a> </div> </div> <!--end slide--> <div class="slidePromo"> <div class="promoImg"><a href="#l"><img src="<?php echo $row2['image'];?>"/></a></div> </div> </div> <!--end slide--> <?php } ?> No idea why you've got multiple queries and then putting it into an array. Either way that should sort your issue... Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 27, 2012 Share Posted March 27, 2012 In the above example, I don't see $row ever defined. It is not going to be easy to do it while you're selecting the data, you want to select the data then loop through it. Quote Link to comment Share on other sites More sharing options...
cpd Posted March 27, 2012 Share Posted March 27, 2012 In the above example, I don't see $row ever defined. It is not going to be easy to do it while you're selecting the data, you want to select the data then loop through it. Thank you for pointing that out, was a typo. I just copied his code as you can see and made a few quick adjustments. Regarding your second statement, there's no reason not to loop through the data as per the example. From what I understand his issue is, the example is a logical solution. Quote Link to comment Share on other sites More sharing options...
azukah Posted March 28, 2012 Author Share Posted March 28, 2012 sorry for the messy code, i was trying different things and forgot to delete some stuff. i cleaned it up and changed it to a for loop and it works Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 28, 2012 Share Posted March 28, 2012 Your example will still print each image twice, as he described in the original problem. OP, can you post your new code to demonstrate the solution? Quote Link to comment 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.