Jump to content

help to separate query array


Go to solution Solved by Guber-X,

Recommended Posts

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));
Link to comment
https://forums.phpfreaks.com/topic/281936-help-to-separate-query-array/
Share on other sites

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

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 by denno020
  • Solution

whoa... some quick replies from my last post :P...

 

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

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.