Guber-X Posted September 6, 2013 Share Posted September 6, 2013 so I have a query that I need to basically split in half to display in two section on the web page. I have a left side and a right side that will be filled with random selected images from the database. <body> <div class="leftbar"> 4 images here </div> <div class="maincontent"> main content of viewed page </div> <div class="rightbar"> next 4 images here </div> </body> is there a way this can be done? the query i would be using would look like this... except I need to spit the results in half to display in the two "leftbar" and "rightbar" sections $images = mysqli_query($con, "SELECT * FROM pictures ORDER BY RAND() LIMIT 8") or die("Image Query Failed: ".mysqli_error($con)); Quote Link to comment https://forums.phpfreaks.com/topic/281936-help-to-separate-query-array/ Share on other sites More sharing options...
requinix Posted September 6, 2013 Share Posted September 6, 2013 You don't have to really "split" anything. Just make the leftbar read four results and the rightbar read whatever is remaining. Quote Link to comment https://forums.phpfreaks.com/topic/281936-help-to-separate-query-array/#findComment-1448508 Share on other sites More sharing options...
Guber-X Posted September 7, 2013 Author Share Posted September 7, 2013 do you have a example of doing this? ive been searching and trying out things for the past few days now and am having no luck... Quote Link to comment https://forums.phpfreaks.com/topic/281936-help-to-separate-query-array/#findComment-1448569 Share on other sites More sharing options...
vinny42 Posted September 7, 2013 Share Posted September 7, 2013 The queryresult can be read using a loop, so you can put all the results in an array. Then for the left side you loop through the first four elements of that array, and for the right side you loop through the other four. Don't think too much about the problem, just do exactly what you want ro achieve: "stick the first four here, and the other four there." By the way, unless this is an extremely highperformance thing, you could also just run two queries, one for the left and one for the right: the first would do LIMIT 4 OFFSET 0 and the other: LIMIT 4 OFFSET 4 Quote Link to comment https://forums.phpfreaks.com/topic/281936-help-to-separate-query-array/#findComment-1448572 Share on other sites More sharing options...
denno020 Posted September 7, 2013 Share Posted September 7, 2013 (edited) you could use a loop like this: <body> <div class="leftbar"> <?php $count = 0; foreach($results as $img){ count++; echo $img; if(count == 4){ echo ' </div> <div class="maincontent"> <!-- main content of viewed page --> </div> <div class="rightbar"> '; } } ?> </div> </body> It's a little ugly, but it will give you what you want. Basically you just loop through your 8 results, keeping track of how many have been printed, once you've hit 4, print out the end of the left column div, the main content, the start of the right column div, and then let the loop finish. As I said, this isn't the best way to do it, but it's one way. Hope that helps. Denno Edited September 7, 2013 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/281936-help-to-separate-query-array/#findComment-1448574 Share on other sites More sharing options...
Solution Guber-X Posted September 7, 2013 Author Solution Share Posted September 7, 2013 whoa... some quick replies from my last post ... for what was a mind boggling issue for me and a few days trying to get this to work, I finally have got a solution for my problem. you could use a loop like this.... etc. you had the right idea, here is what solved it for me <div id="contain"> <div id="leftbar"> <div class="block"> <?php $img_result = mysqli_query($con, "SELECT * FROM pictures ORDER BY RAND() LIMIT 8") or die("img_result FAILED: ".mysqli_error($con)); $i = 0; while($i < 4){ while($row = mysqli_fetch_array($img_result)){ extract($row); echo "<img src=\"images/pictures/".$thumb."\" width=\"150px\" />"; $i++; if($i==4) break; } } ?> </div> </div> <div id="Main"> MAIN CONTENT HERE </div> <div id="rightbar"> <div class="block"> <?php while($row = mysqli_fetch_array($img_result)){ extract($row); $i++; if($i>4){ echo "<img src=\"images/pictures/".$thumb."\" width=\"150px\" />"; } } ?> </div> </div> </div> I am only testing this with 5 images in my DB.. hope this will work fine when it becomes loaded with lots of images Quote Link to comment https://forums.phpfreaks.com/topic/281936-help-to-separate-query-array/#findComment-1448578 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.