Note that there are some explanatory texts on larger screens.

plurals
  1. POSplit mySQL results over two <div> and limit to 4
    primarykey
    data
    text
    <p>I have a question that relates to adverts that are stored in a mySQL database. I need to fetch all the results, sort by the id descending limit to 8 and then display 4 results in one &lt;div&gt; and the other 4 in another &lt;div&gt;.</p> <p>Here's my current code:</p> <pre><code> &lt;div id="first_adverts"&gt; &lt;?php $i = 1; ?&gt; &lt;?php $getAdverts=mysql_query(" SELECT * FROM adverts WHERE live = 1 ORDER BY id DESC LIMIT 8"); while($showAdverts=mysql_fetch_array($getAdverts)) { $checkdate = $showAdverts['expiry_date']; // Date From Advert in Database $checkdate = strtotime(str_replace("/","-",$checkdate)); // Change Date format to 01-07-2013 instead of 01/07/2013 if ($checkdate &gt; time()) { // If date is in the future (EG the advert hasn't expired) then show: $showAdvertiserData=mysql_fetch_array(mysql_query(" SELECT * FROM advertisers WHERE id = '".$showAdverts['advertiser_id']."'")); ?&gt; &lt;div class="advert-cell"&gt; &lt;a href="#" class="topopup_&lt;?php echo $i; ?&gt;"&gt; &lt;img src="images/adverts/&lt;?php echo $showAdverts['image']; ?&gt;" width="200"&gt; &lt;/a&gt; &lt;/div&gt; &lt;div id="toPopup_&lt;?php echo $i; ?&gt;"&gt; &lt;div class="close"&gt;&lt;/div&gt; &lt;div id="popup_content"&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h1&gt;Contact &lt;span class="green"&gt;&lt;?php echo $showAdvertiserData['company_name']; ?&gt;&lt;/span&gt;&lt;/h1&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;form id="advertenquiry&lt;?php echo $i; ?&gt;" name="advertenquiry&lt;?php echo $i; ?&gt;" method="post" action="" onSubmit="return validateAdvertEnquiryForm&lt;?php echo $i; ?&gt;()"&gt; &lt;input name="advertiser_id" type="hidden" value="&lt;?php echo $showAdverts['advertiser_id']; ?&gt;"&gt; &lt;input name="advert_id" type="hidden" value="&lt;?php echo $showAdverts['id']; ?&gt;"&gt; &lt;p&gt;&lt;input name="enquiry_name" type="text" id="name" value="Name *" onFocus="clearMe(this)" style="width: 250px;" /&gt;&lt;/p&gt; &lt;p&gt;&lt;input name="enquiry_telephone" type="text" id="telephone" value="Telephone *" onFocus="clearMe(this)" style="width: 250px;" /&gt;&lt;/p&gt; &lt;p&gt;&lt;input name="enquiry_email" type="text" id="email" value="Email *" onFocus="clearMe(this)" style="width: 250px;" /&gt;&lt;/p&gt; &lt;p&gt;&lt;textarea name="enquiry_query" id="query" cols="45" rows="5" onFocus="clearMe(this)" style="width: 300px;" &gt;Query &lt;/textarea&gt;&lt;/p&gt; &lt;p&gt;&lt;input type="submit" name="enquire" value="Send" class="submit_button" /&gt;&lt;/p&gt; &lt;/form&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;?php $i++; ?&gt; &lt;?php } } ?&gt; &lt;/div&gt; &lt;div id="second_adverts"&gt; &lt;?php // second set of ads here ?&gt; &lt;/div&gt; </code></pre> <p>As you can see this shows all 8 results and then checks to see if the advert's expiry date is in the future or not. Basically I need the above code to show the first 4 results and then the last 4 separately later on the page. (There would be a bit more HTML in between the two queries)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload