Jump to content

help to separate query array


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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.