Reputation: 786
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 <div> and the other 4 in another <div>.
Here's my current code:
<div id="first_adverts">
<?php $i = 1; ?>
<?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 > 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']."'")); ?>
<div class="advert-cell">
<a href="#" class="topopup_<?php echo $i; ?>">
<img src="images/adverts/<?php echo $showAdverts['image']; ?>" width="200">
</a>
</div>
<div id="toPopup_<?php echo $i; ?>">
<div class="close"></div>
<div id="popup_content">
<p> </p>
<p> </p>
<h1>Contact <span class="green"><?php echo $showAdvertiserData['company_name']; ?></span></h1>
<p> </p>
<p> </p>
<form id="advertenquiry<?php echo $i; ?>" name="advertenquiry<?php echo $i; ?>" method="post" action="" onSubmit="return validateAdvertEnquiryForm<?php echo $i; ?>()">
<input name="advertiser_id" type="hidden" value="<?php echo $showAdverts['advertiser_id']; ?>">
<input name="advert_id" type="hidden" value="<?php echo $showAdverts['id']; ?>">
<p><input name="enquiry_name" type="text" id="name" value="Name *" onFocus="clearMe(this)" style="width: 250px;" /></p>
<p><input name="enquiry_telephone" type="text" id="telephone" value="Telephone *" onFocus="clearMe(this)" style="width: 250px;" /></p>
<p><input name="enquiry_email" type="text" id="email" value="Email *" onFocus="clearMe(this)" style="width: 250px;" /></p>
<p><textarea name="enquiry_query" id="query" cols="45" rows="5" onFocus="clearMe(this)" style="width: 300px;" >Query </textarea></p>
<p><input type="submit" name="enquire" value="Send" class="submit_button" /></p>
</form>
<p> </p>
</div>
</div>
<?php $i++; ?>
<?php } } ?>
</div>
<div id="second_adverts">
<?php // second set of ads here ?>
</div>
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)
Upvotes: 1
Views: 1666
Reputation: 7805
You could do that with a single query:
$query = "SELECT 'adverts.id', 'adverts.advertiser_id', 'adverts.image' 'advertisers.company_name'
FROM adverts
LEFT JOIN advertisers ON adverts.advertiser_id = advertisers.id
WHERE((expiry_date > NOW()) && (live = 1))
ORDER BY id DESC LIMIT 8";
$count = 0;
while($showAdverts=mysql_fetch_array(mysql_query($query)) {
if ($count < 4){
//display first four
} else {
//display second four
}
$count++;
}
Upvotes: 2
Reputation: 1592
Why do you not just make an if (i <= 4) -> write it in the first div or (else) write it in the second div?
Upvotes: 1
Reputation: 3078
Here's one solution I used earlier in my project.
1) Fetch all the record and store into a php array
$rows = array();
while($row = mysql_fetch_array(YOUR_QUERY))
{
$rows[] = $row;
}
2) Now Using foreach()
loop use data fetched from query where ever you want
if(is_array($rows)) {
foreach($rows as $row) {
//do with $row or create some if,switch condition here !
}
}
For specific limits and tweaks study the result set we get from mysql_fetch_array()
!
Upvotes: 1