How To Display random ads in my web site - php

Hi I want to display random ads in my product page, my all products call in the while loop to display in home page, i want to add ads from backend in between these products randomly in same design as product display like in freekaamaal
How I display these ads in between products can you give me the idea ??
see this for more info
<div class="products-grids">
<div class="col-md-12">
<?php
$id=$_GET['id'];
$result1 = mysqli_query($con,"select * from products1 where sta='Active' order by id DESC LIMIT 0 , $resultsPerPage");
while($ro = mysqli_fetch_array($result1))
{
$nam1=substr($ro['pne'],0,60);
$url1=$ro['url'];
$description1=substr($ro['description'],0,200);
$price1=$ro['price'];
$price11=$ro['price1'];
$bid1=$ro['company'];
$image=$ro['image_name'];
$time=$ro['time'];
$tag=$ro['tag'];
?>
<div class="col-md-3">
<div class="hentry post1 id="post-225396">
Display Product in loop
</div>
</div>
<?php } mysqli_close($con);?>
<!-- <div class="col-md-3">
<div class="hentry post1 id="post-225396"><img src="ads.jpg"></div>
</div> -->
</div>
<div class="clearfix"> </div>

Anyways your question is too broad.
Here is step by step solution.
FRONTEND HTML + PHP
// TWO HARD DIV'S + THREE RANDOM ADS , BUT ONLY ONE OF THE AD GONNA TRIGGER.
<?php $random_number = rand(0,2); ?>
<?php ads($random_number,0); ?>
<div>your image or whatever</div>
<?php ads($random_number,1); ?>
<div>your image or whatever</div>
<?php ads($random_number,2); ?>
<?php $random_number = rand(0,2); ?>
<?php ads($random_number,0); ?>
<div>your image or whatever</div>
<?php ads($random_number,1); ?>
<div>your image or whatever</div>
<?php ads($random_number,2); ?>
COMPARISION FUNCTION
// Now create a function to compare if random number generated is equal to ad's placeholder.
function ads($rand_num, $placeholder){
if($rand_num == $placeholder){
echo "<div> YOUR ADS CONTENT </div>";
}
}
Note : It's not meant to run perfectly , it's just a outline on how to achieve that specific goal.

Do something like this (i tried to code it to be easy to understand):
<?php
function show_an_ad()
{
$the_ad = '';
switch (mt_rand(0,2))
{
case 0:
$the_ad = 'ad0.jpg';
break;
case 1:
$the_ad = 'ad1.jpg';
break;
case 2:
$the_ad = 'ad2.jpg';
break;
}
echo '<div class="col-md-3"><div class="hentry post1"><img src="'.$the_ad.'"></div></div>';
}
$ads_every_how_many_products = 3;
<div class="products-grids">
<div class="col-md-12">
<?php
$id=$_GET['id'];
$result1 = mysqli_query($con,"select * from products1 where sta='Active' order by id DESC LIMIT 0 , $resultsPerPage");
$counter = 1;
while($ro = mysqli_fetch_array($result1))
{
$nam1=substr($ro['pne'],0,60);
$url1=$ro['url'];
$description1=substr($ro['description'],0,200);
$price1=$ro['price'];
$price11=$ro['price1'];
$bid1=$ro['company'];
$image=$ro['image_name'];
$time=$ro['time'];
$tag=$ro['tag'];
?>
<div class="col-md-3">
<div class="hentry post1" id="post-225396">
Display Product in loop
</div>
</div>
<?php
if(!($counter%$ads_every_how_many_products))
{
show_an_ad();
}
$counter++;
}
mysqli_close($con);
?>
</div>
</div>
<div class="clearfix"> </div>
i don't have a php system here so there's a chance there could be a syntax error or something

Related

Keep columns in the same row until header result is different using while loop

I would like to display as shown below, but I can't get to anything that will display addresses that belongs to a certain state, side by side in the same row, until the states changes.
<h1>State1</h1>
<div class="row">
<div class="col-sm-3">
<h2>City1</h2>
<p>Address1</p>
</div>
***if next address is in the same state:
<div class="col-sm-3">
<h2>City2</h2>
<p>Address2</p>
</div>
</div><!-- ROW -->
<hr>
***addresses will be in the same row until state is different, then close the div.
**if next address is NOT in the same state
<h1>State2</h1>
<div class="row">
<div class="col-sm-3">
<h2>City3</h2>
<p>Address3</p>
</div>
...
</div> <!-- ROW -->
I'm using:
<?php
$query = "SELECT * FROM places";
$select_all_places = mysqli_query($connection, $query);
$current_state = '';
while($row = mysqli_fetch_assoc($select_all_places)){
$p_state = $row['p_state'];
$p_city = $row['p_city'];
$p_address = $row['p_address'];
?>
<?php if($current_state != $p_state): ?>
<h1><?php echo $p_state; ?></h1>
<?php $current_state = $p_state; ?>
<div class="row">
<?php endif; ?>
<div class="col-sm-3">
<h2><?php echo $p_city; ?></h2>
<p><?php echo $p_address; ?></p>
</div>
</div>
<?php } ?>
You need to order addresses by state, to make sure that you list all of one state and only then go to another state addresses.
This code will do it.
<?php
$select_all_places = mysqli_query($connection,"SELECT * FROM places ORDER BY p_state ASC");
$current_state = '';
while($row = mysqli_fetch_assoc($select_all_places))
{
if($current_state != $row['p_state'])
{
if($current_state!='')
{
echo'</div>';
}
$current_state = $row['p_state'];
echo '<h1>'.$row['p_state'].'</h1><div class="row">';
}
echo'<div class="col-sm-3"><h2>'.$row['p_city'].'</h2><p>'.$row['p_address'].'</p></div>';
}
?>
It takes sorted addresses from database.
Loop trough them and show H1 header then state changes.
If there was a state list earlier, closes div element of previous state.

Pagination script added php page isn't loading or showing any error

I tried to add a pagination script to my existing php page of sql queries.
But after adding the script the page is kept on loading without showing any content or error.
My code goes as:
<?php include('db.php'); ?>
<?php // define how many results you want per page
$results_per_page = 10;
// find out the number of results stored in database
$sql10='SELECT * FROM smf_messages';
$result10 = mysqli_query($conn, $sql10);
$number_of_results = mysqli_num_rows($result10);
// determine number of total pages available
$number_of_pages = ceil($number_of_results/$results_per_page);
// determine which page number visitor is currently on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
// determine the sql LIMIT starting number for the results on the displaying page
$this_page_first_result = ($page-1)*$results_per_page;
?>
Now the sql query codes to get the data from the respective tables...
<?php
$sql2 = "SELECT * FROM smf_log_digest WHERE note_type = 'topic' ORDER BY id_msg DESC LIMIT 420";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
$number = $row2["id_msg"];
?>
This query relates to the content from which table to be retrieved..
<?php
// retrieve selected results from database and display them on page
$sql20='SELECT * FROM smf_messages WHERE id_msg = $number AND id_board = 4 LIMIT ' . $this_page_first_result . ',' . $results_per_page;
$result20 = mysqli_query($conn, $sql20);
while($row20 = mysqli_fetch_array($result20)) {
$member = $row20["id_member"];
$replies = $row20["id_topic"];
?>
<?php
$sqlstep1 = "SELECT COUNT(*) AS total FROM smf_log_digest WHERE note_type = 'reply' AND id_topic = $replies";
$rowNumstep1 = mysqli_query($conn, $sqlstep1);
$countstep1 = mysqli_fetch_assoc($rowNumstep1);
?>
// Body
<article class="well btn-group-sm clearfix">
<div class="topic-desc row-fluid clearfix">
<div class="col-sm-2 text-center publisher-wrap">
<img src="assets/images/profile.png" alt="" class="avatar img-circle img-responsive">
<h5><?php echo $row3["poster_name"]; ?></h5>
<small class="online">Member</small>
</div>
<div class="col-sm-10">
<header class="topic-footer clearfix">
<!-- end tags -->
</header>
<!-- end topic -->
<h4> <?php echo $row20["body"]; ?></h4>
<div class="blog-meta clearfix">
<small><?php echo $countstep1["total"]; ?> Replies</small>
<small><?php echo date('m/d/Y', $row20["poster_time"]); ?></small>
</div>
View the topic →
</div>
</div>
</article>
//end of body
<?php
}
// display the links to the pages
for ($page=1;$page<=$number_of_pages;$page++) {
echo '' . $page . ' ';
}
?>
<?php }
} else {
echo "";
}
?>
Please note that the data base connections are all checked and are right..
Any help is appreciated..
add this on top then check for error.
error_reporting(E_ALL);
ini_set('desplay_errors','1');

How to limit the number of 'Recent Blogs' created in CakePHP?

I'm trying to create a blog, and in every single blog it shows the recent blogs that has been created on the bottom of the page. Is there a way i can limit this number to 4 recent blogs? Because currently all the blogs that has been created show's up on the "Recent Blogs" area when i generate it.
<div class="container" id="newsextra">
<h4>MORE NEWS</h4>
<div class="row">
<?php
if(!empty($error)){
echo $error;
}
if (!empty($blogsinfos)) {
foreach ($blogsinfos as $blogs): ?>
<div class="col-md-3">
<a href="/news-single/<?= h($blogs->id)?>">
<img src="<?= h($blogs->mainimg)?>" class="img-responsive">
<h5><?= h($blogs->title)?></h5>
<h6><?= h($blogs->created)?></h6>
</a>
</div>
<?php
endforeach;
}
?>
</div>
</div>
From the controller you should limit the number of posts generated. Thanks #Rik.esh for the answer.
$this->loadModel('Blogs');
$opts1['conditions'] = array('Blogs.status' => 1);
$opts1['limit'] = 4;
$opts1['order'] = array('Blogs.created' => 'desc');
$blogsinfos = $this->Blogs->find('all',$opts1);
$this->set('blogsinfos', $blogsinfos);
$this->set('_serialize', ['blogsinfos']);
foreach ($blogsinfos as $blogs) {
$proid = $blogs['id'];
}
try this
$opts1['order'] = 'Blogs.created desc';

Change column size based on amount of elements

So I'm working on a HTML carousel using Twitter Bootstrap, Wordpress and ACF Fields.
This carousel shows 2 items per row. Each of these items has a class of "col-md-6". So by showing 2 items per row, the total is 2 columns of "col-md-6" (which is perfect since this completes the 12 columns required by Bootstrap):
Here is my code:
<?php if (have_rows('columns_carousel_slide')) {
$count = 0; ?>
<div class="item active"><div class="row">
<?php while(have_rows('columns_carousel_slide')) {
the_row();
if ($count > 0 && (($count % 2) == 0)) {
?>
</div> <!--.item -->
</div> <!--.row -->
<div class="item">
<div class="row">
<?php } ?>
<div class="col-md-6">
<h2><?php the_sub_field('columns_carousel_slide_title'); ?></h2>
</div> <!--.col-md-6 -->
<?php $count++; } ?>
</div> <!--.item -->
</div> <!--.row -->
<?php } ?>
However, I would like to know if there's a way to detect if there's 1 item per row and if so, then show a "col-md-12" instead of a "col-md-6" in order to fill in the remaining space of not having 2 items.
Any ideas are welcome.
Thanks!
--
Edit: As suggested by Jakub, I've updated my code to the following:
<?php if (have_rows('columns_carousel_slide')) {
$count = 0; ?>
<div class="item active"><div class="row">
<?php
$multiplier = 1; //that could actually go before the while
if (count(get_field('columns_carousel_slide'))%2 === 1 &&
$count === count(get_field('columns_carousel_slide'))-1) {
$multiplier = 2;
} ?>
<?php while(have_rows('columns_carousel_slide')) {
the_row();
if ($count > 0 && (($count % 2) == 0)) {
?>
</div> <!--.item -->
</div> <!--.row -->
<div class="item">
<div class="row">
<?php } ?>
<div class="col-md-<?php echo (6*$multiplier);?>">
<h2><?php the_sub_field('columns_carousel_slide_title'); ?></h2>
</div> <!--.col-md-6 -->
<?php $count++; } ?>
</div> <!--.item -->
</div> <!--.row -->
<?php } ?>
However, I think I've must have missed something because I am getting total of "col-md-12" for all the rows.
Assuming that "get_field" returns the array with all rows, then you would need to change the following:
<div class="col-md-6">
<h2><?php the_sub_field('columns_carousel_slide_title'); ?></h2>
</div> <!--.col-md-6 -->
with this:
<?php
$multiplier = 1; //that could actually go before the while
if (count(get_field('columns_carousel_slide'))%2 === 1 &&
$count === count(get_field('columns_carousel_slide'))-1) {
$multiplier = 2;
} ?>
<div class="col-md-<?php echo (6*$multiplier);?>">
<h2><?php the_sub_field('columns_carousel_slide_title'); ?></h2>
</div> <!--.col-md-12 -->
A short explanation:
initially we set the multiplier to 1 (so that 6*1 = 6 for paired
columns).
we do a condition to check if number of items is even
and we are currently processing the last item. If so - we set the
multiplier to 2 (so that 6*2 = 12 for single column)
we set the class to be "col-md-" and the result of our calculation (either 6 or 12)

How to select two rows each time from a table in SQL and display them in PHP?

I have a table with fields like id, date, heading, news. I want to display the fields date, heading and news in an Owl Carousel slider. Can anyone suggest how to pick two rows at a time from an SQL table to display two entries at a time in the Owl Carousel slider. I have used an SQL query like this:
<?php
$sql3 = "SELECT date, heading, news FROM news ORDER BY news.date LIMIT 0, 1";
$result3 = mysql_query($sql3) or die(mysql_error());
while($row3 = mysql_fetch_array($result3)) {
?>
<div class="item ">
<div class="l_blk">
<div class="news_container">
<div class="col-md-1 date_b"><p>Mar<br>09</p></div>
<div class="col-md-11 cont_b">
<p>
<span class="news_t">"<?php echo $row3['heading']; ?>"</span><br>
<?php echo $row3['news']; ?>
</p>
</div>
</div>
</div>
<div class="l_blk">
<div class="news_container">
<div class="col-md-1 date_b"><p>Mar<br>09</p></div>
<div class="col-md-11 cont_b">
<p>
<span class="news_t">"<?php echo $row3['heading']; ?>"</span><br>
<?php echo $row3['news']; ?>
</p>
</div>
</div>
</div>
</div>
<?php
}
?>
Can anyone suggest how to do this?
$sql3 = "SELECT date, heading, news FROM news ORDER BY news.date LIMIT 0, 1";
Don't you change the LIMIT 0, 2
Which means select 2 starting with the value at index 0?
Once you've done that, take out the duplicate code inside the loop
Try this pls.
<?php
$sql3 = "SELECT date, heading, news FROM news ORDER BY news.date"; // No limit
$result3= mysql_query($sql3) or die(mysql_error());
$counter = 0;
while($row3 = mysql_fetch_array($result3, MYSQL_ASSOC))
{
if($counter % 2 == 0){
echo '<div class="item ">'.PHP_EOL;
}
?>
<div class="l_blk">
<div class="news_container">
<div class="col-md-1 date_b"><?php $row3['date'] ?></div>
<div class="col-md-11 cont_b">
<p>
<span class="news_t">"<?php echo $row3['heading']; ?>"</span><br>
<?php echo $row3['news']; ?>
</p>
</div>
</div>
</div>
<?php
if($counter % 2 == 1){
echo '</div>'.PHP_EOL;
}
$counter++;
}
if($counter % 2 == 1){
echo '</div>'.PHP_EOL;
}
?>

Categories