Below is the code to display the post. That loads every 10 posts. And I want to display banner after 3 post. Can someone tell me, code to display banner after 3 post? By completing the code below. Thank you so much for your help.
<?php
$stories = Wo_GetPosts(array('limit' => 10));
if (count($stories) <= 0) {
echo Wo_LoadPage('story/no-stories');
} else {
foreach ($stories as $wo['story']) {
echo Wo_LoadPage('story/content');
}
}
?>
You are trying to use count() in a way that this function is not intended. Per the PHP documentation:
Counts all elements in an array, or something in an object.
http://php.net/manual/en/function.count.php
What you need to do is create a counter variable that increments within your foreach loop, and when it hits 3 outputs the banner. Your code might look something like this:
<?php
$stories = Wo_GetPosts(array('limit' => 10));
// No stories; output error
if (count($stories) <= 0)
echo Wo_LoadPage('story/no-stories');
// Stories exist; show them!
} else {
$count = 0;
// Loop through $stories
foreach ($stories as $wo['story']) {
// Increment the value of $count by +1
$count++;
if ($count == 3) {
// Output my Banner here
}
echo Wo_LoadPage('story/content');
}
}
?>
One thing I would note is that the above code only outputs a banner one time; when the value of $count is 3. You could adjust this to run ever 3rd story by changing the match from if ($count ==3) to if ($count % 3 == 0) which essentially reads as If the value of $count is divisible by 3.
I guess you create some HTML Code with this? If true, I'd use some Javascript for this purpose. Something like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="stories">
<div>story 1</div>
<div>story 2</div>
<div>story 3</div>
<div>story 4</div>
</div>
<script type="text/javascript">
$("#stories>div:nth-child(3)").after('<div class="banner">your bannery</div> ');
</script>
</body>
</html>
Related
These are my problems:
Note: the single queotes (') are not actually displayed
echo $variable; returns nothing
echo "$variable" returns '$variable'
returns 'stuff"; code ?'
this problem appears both in a portable version of Chrome and in Firefox
here is my code for reference:
<!DOCTYPE HTML>
<html lang = "it" >
<head>
<meta charset="utf-8">
<meta name = "keywords" content = "catcher, gioco, top, 10">
<meta name="author" content="Luca Ballarati">
<?php require 'connessione'; ?>
<title> TOP 10 </title>
</head>
<body style = "background-color: white;">
<h1>PROVA:</h1>
<?php
apri_conn();
mysqli_select_db($con,"catcher");
$sql="SELECT Username, Record FROM utenti ORDER BY Record LIMIT 10";
$result = mysqli_query($con,$sql);
chiudi_conn();
echo '<b><center><h2>TOP 10 GIOCATORI e RECORD</h2></center></b><br><br>.';
?>
<?php
$i=0;
while ($i < 11) {
$i++;
$NomeUtente = mysql_result($result,$i,"NomeUtente");
$Record = mysql_result($result,$i,"Record");
$str = "AAA"
echo "<p>AAA</p>";
}
?>
</body>
I think I'm using the correct syntax, from the comparisons I made with other posts on this site and others. Am I wrong, or is there something else at play?
Apart from you numerous errors contained within this code, you shouldn't use echo on a HTML document, you can simply close the PHP tag. For example
if( 1 == 1 ) { //True every time
?>
<p>AAA</p>
<?php } ?>
is almost the same as you would write :
if( 1 == 1 ) { //True every time
echo "<p>AAA</p>";
}
Also if you want to mix HTML with PHP code, I would advise you to use endif and endwhile for your loops.
Removed (by original poster) due to unclear formulation of question.
More accurate question: Create divs associated to objects
for ($i = 0; $i < $amountOfPersons; $i++)
{
?>
<div class="fixed-size-square">
<span><?php echo $someContentThatBelongsToPerson[$i]; ?></span>
</div>
<?php
}
Assuming you'll get your database results returned to you in an array, the common way of doing this would be this in PHP:
foreach( $persons as $person ){
echo '<div class="fixed-size-square">
<span>I\'m a 200 x 200 px square with centered content no matter how many lines it takes</span>
</div>';
}
You could do it with the $amountPersons variable too (although I don't know why you'd want that if you get a result set back from the database), like this:
for ( $i = 0, $i < $amountPersons, $i++ ){
echo '<div class="fixed-size-square">
<span>I\'m a 200 x 200 px square with centered content no matter how many lines it takes</span>
</div>';
}
edit:
By the way, re-reading your question I realized you want to eventually put names in your divs, presumably the names stored in your database. You can do that with the first example, like this (this assumes the column name is your database is personName and that your result set is an array of objects.):
foreach( $persons as $person ){
echo '<div class="fixed-size-square">
<span>Name: '.$person->name.'</span>
</div>';
}
Since you have not tagged this as JavaScript, I can only assume you want a PHP approach to this, when you build the page I can only assume that you are running a query to get the records from the DB (since no code is provided).
All you need to do is wrap your div inside a for loop like this.
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
for($i = 0; $i < $AmountPersons; $i++){
echo '
<div class="fixed-size-square">
<span>I\'m a 200 x 200 px square with centered content no matter how many lines it takes</span>
</div>
';
}
?>
</body>
</html>
Hope this helps!
Try this
for ($i = 0; $i < $amountOfPersons; $i++) { ?>
<div class="fixed-size-square">
<span><?php echo $someContentThatBelongsToPerson[$i]; ?></span>
</div>
<?php } ?>
I implemented Yii infinite scroll its working fine. When I added Google ads based on given condition its displaying four Google ads. Then when fetching next page, the values are repeating. ie (Google ads-showing empty results) so that I want to GET the infinite scroll page ID.
Please help me how to get the page ID(ie next page ID). I have added my code here :
<?php
$post_counter = 0;
$addnum = 0;
foreach($posts as $rec): ?>
<div class="post">
<?php $rec_id = $rec['recipe_id']; ?>
$post_counter++;
if (($post_counter == 7) AND ($addnum < 4)) {
$addnum = $addnum + 1;
$post_counter=1;
}
?>
<script>
// Display an ad unit
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<?php if(isset($addnum)>3)
echo exit($post_counter)?>
<?php
}
?>
i put like this it will work
go to extension/assets/yiiinfinitescroll.js
get ID from js file
I am new to php and I am trying to create a simple bit of code that prints a "last" class at the start of the last element in a "while" loop. There are only two items in the loop (blog excerpts) hence why I have tried below with the if ($i == 1)... Thanks for any help.
Here is my code so far - which only returns the p
<?php
$i = 0;
if($i == 1) {
echo '<p class="last">';
}
else {
echo '<p>';
}
?>
EDIT:
Thanks for the help so far. Greatly appreciated - I have provided a bit more information below (I posted late at night, so I realise I haven't been all that clear).
This is the full piece of code I am trying to write. It is pulling blog excerpts from Wordpress - currently limited to 2 blog articles.
<?php
$posts = new WP_Query('showposts=2');
while ( $posts->have_posts() ) : $posts->the_post();
?>
<p><?php echo the_title(); ?><br/>
<?php echo substr(get_the_excerpt(), 0,200); ?>... Read More</p>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I am wanting to add the class "last" to the p at the start of line 5 - for the last blog except only.
Thanks again.
Nick's answer says almost all that needs to be said.
The only thing I might add is a slight variation to save duplication particularly if the the contents of your paragraph tags is more complicated.
This might be better done with the following tweak on Nick's code:
<style>
#contents p:last-child {
PUT CONTENTS OF CLASS HERE
}
</style>
<body>
<div id="#contents">
<?php
$numLoops = 2;
$ctext=""
for($i=0; $i<$numLoops; $i++) {
$info="whatever";
if($i == (numLoops-1)) {
$ctext=' class="last"';
}
echo "<p${ctext}>${info}</p>\n";
}
?>
</div>
</body>
Cheers
So you have two paragraphs, and you want to apply the class "last" to the last one? Sounds like this is better handled with CSS
<style>
#contents p:last-child {
PUT CONTENTS OF CLASS HERE
}
</style>
<body>
<div id="#contents">
<p> first info</p>
<p> last info </p>
</div>
</body>
OR if you want to learn about loops
<?php
$numLoops = 2;
for($i=0; $i<$numLoops; $i++) {
if($i == (numLoops-1)) {
echo '<p class="last">';
} else {
echo '<p>';
}
}
What we are doing here with the for loop is initially setting the variable $i=0; then setting a test that says keep looping as long as the variable is less than the number of loops we want do do. Next we are setting what to do each loop, in this case we increment our variable by one each time.
First loop
i=0, we see that 0 is < 2 so we continue
Second loop
We execute the $i++ so we increment $i by 1 from 0 to $i=1,
we test and see $i=1 is still less than 2 so we continue.
Third attempted loop
We increment $i by 1 and get $i=2.
We test to see if this is less than 2, but it is NOT so we do not execute code in the loop
The main issue is that you don't have a loop in your code, and if you did you aren't incrementing your variable $i
I'm taking a break from Project Euler to learn some PHP/HTML for kicks and giggles, and I found a page of simple exercises. So, on my 'site,' I want to have an ordered list of links to pages of each of the exercises, but I decided to do it in a dynamic manner as opposed to hard coding each item as I do the exercise. Unfortunately, the page that should contain the list doesn't render at all!
Assuming I have files on my system with the names "exawk#.php," what else could be wrong with this code? Sorry if it is sloppy or horrible, it's literally my first day of web programming.
<html>
<head>
<title> Awaken's Exercises </title>
</head>
<body>
<h1>This page contains "Awaken's Exercises" from
<a href="http://forums.digitalpoint.com/showthread.php?t=642480">
this page</a>.</h1>
<ol>
<?php
$arex = glob("exawk*.php"); // $arex contains
//an array of matching files
$numex = 0;
$i = 0;
foreach( $arex )
{
$numex++;
}
while( $numex >= 0 )
{
echo "<li><a href=" .$arex[$i].
">Problem #" .$numex. ".</a></li>";
$numex--;
$i++;
}
?>
</ol>
</body>
</html>
Enable display_errors in php.ini: foreach( $arex ) is a syntax error (missing .. as $varname).
From the commandline, you could check it with php -l /path/to/your/file.php.
Also, this sample:
//an array of matching files
$numex = 0;
foreach( $arex as $youdontdoanythingwiththis)
{
$numex++;
}
Could be:
$numex = count($arex);
Better the whole thing:
while( $numex >= 0 )
{ ...etc
Could be:
$num = 1;
foreach($arex as $file){
echo '<li>Problem #'.$num.'</li>';
$num++;
}