php limit what happens in 'while' loop - php

I have some code that creates a divs for each result of a query. My problem is that, I only want the same amount of divs created for the result set I'm limiting.
For example, this query produces about 8-9 rows however, in this case, if $busroute==2, and there are only 2 results that match this, I only want to to create 2 divs and close them. Right now, as I have it, it's creating a div - /div set for each iteration of the "while" loop.
<?php
mysql_data_seek( $result,0);
while ($i2=mysql_fetch_array($result)) {
$busnumber=$i2['NAME'];
$busroute=$i2['route'];
$busdirection=$i2['direction'];
$timediff=$i2['TimeDiff'];
$timestamp=$i2['TIMESTAMP'];
if ($busroute=='2'){
echo "<div class='businfo'>";
echo "<p>Bus #: ".$busnumber."<br>Direction: ".$busdirection."<br>Bus Route : ".$busroute."<br>";
echo "Timestamp : $timestamp</p>";
}
echo "</div>";
}
?>
So, if $busroute==2, and there are two results where $busroute==2, then the following should only output twice.
echo "<div class='businfo'>";
echo "<p>Bus #: ".$busnumber."<br>Direction: ".$busdirection."<br>Bus Route : ".$busroute."<br>";
echo "Timestamp : $timestamp</p>";
}
echo "</div>";
Thanks in advance for any help.
d -

Your code already looks OK for what you want.
I think "</div>" should be inside your IF.
Replace
if ($busroute=='2'){
echo "<div class='businfo'>";
echo "<p>Bus #: ".$busnumber."<br>Direction: ".$busdirection."<br>Bus Route : ".$busroute."<br>";
echo "Timestamp : $timestamp</p>";
}
echo "</div>";
with
if ($busroute=='2'){
echo "<div class='businfo'>";
echo "<p>Bus #: ".$busnumber."<br>Direction: ".$busdirection."<br>Bus Route : ".$busroute."<br>";
echo "Timestamp : $timestamp</p>";
echo "</div>";
}

Related

My onclick events in my php include file do not work

I am working on my college web design course during my easter break and have run into a problem...
I have a menu on each page that I have removed from the html pages and written into a php file and that is used with an include in each page. Everything works fine apart from the onclick events that when clicked SHOULD load another page but at the moment does not do anything.
Here is my php file...
<?php
echo "<div id='leftPnl'>";
echo "<div class='navcon'>";
echo "<button class='static'
onclick='window.location.href='"."./index.html'".">Home</button>";
echo "<button class='accordion'>Gallery</button>";
echo "<div class='panel'>";
echo "<button class='static' onclick='window.location.href='"."./html/countryside.php'".">Countryside</button>";
echo "<div id='socialmedia'>";
echo "<img src='../images/sitewide/map.png'>";
echo "<br>Contact us on...<br>";
echo "<a class='fab fa-facebook-square fa-2x'
onclick='msg('"."Facebook"."')"."></a>";
echo "<a class='fab fa-instagram fa-2x' onclick='msg('"."Instagram"."')".">
</a>";
echo "<a class='fab fa-twitter-square fa-2x'
onclick='msg('"."Twitter"."')"."></a>";
echo "<a class='fab fa-tumblr-square fa-2x' onclick='msg('"."Tumblr"."')".">
</a>";
echo "<div id='clock'>";
echo "<p class='date'>{{ date }}</p>";
echo "<p class='time'>{{ time }}</p>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
on running the file and inspecting the element in Chrome, the url that the href uses is not formatted correctly the ./ are missing and a single quote is also missing.
Is it simply that I am getting confused with the position of my single and double quotes in the php?
Thanks.
PS, I apologise if this has already been asked - I looked and could not find the answer.
Is it simply that I am getting confused with the position of my single and double quotes in the php?
Yes.
Look at the output of the PHP. To take one example:
onclick='msg('Facebook')>
You start the attribute value with '.
Then you try to use ' inside it.
Then you forget the ' at the end!
There is no reason to nest any of this code inside PHP string literals. All that achieves it making it harder to debug.
Just write the HTML directly, outside of <?php ... ?> sections.
It wouldn't be a bad idea to get rid of the intrinsic event attributes too, and use JavaScript to bind event handlers.

WP ACF echoes variable even if not wanted

I have strange problem, i've done the same thing before, many times.
But for some reason this one field just dosent want to co-operate whatsoever...
<?php if (have_rows('subfamilies_product_changes')) :
while ( have_rows('subfamilies_product_changes') ) : the_row();
$url = the_sub_field('subfamilies_product_changes_link');
echo "<div class='support-lahko'>";
echo "<div class='support-logo'>";
echo the_date( 'Y-m-d' );
echo "</div>";
echo "<div class='news-name-product'>";
echo "<h3 style='vertical-align: middle;'>" . the_sub_field('subfamilies_product_changes_title') ."</h3>";
echo "</div>";
echo "<div class='news-desc'>";
echo "</div>";
echo "</div>";
echo "<br>";
endwhile;
endif;
It seems, no matter where i put my <a> tags, this script is just going to go ahead and for some reason echo the link from $url = the_sub_field('subfamilies_product_changes_link'); into where ever it is defined. when i try to do something like <a href=' . $url . '> It will still just echo the url in <p> tags in seemingly random place.
Here is picture of what happens when i put $url inside href:
http://imgur.com/a/7AQsl
href stays empty, and wp just echoes url where it was defined. none of the text in image is a link.
I would just need to echo the url INTO the href, so i can iterate links into pdf files.
the_sub_field('subfamilies_product_changes_title')
produces text: Test Changes, as expected.
Short answer - use get_sub_field();
the_sub_field() outputs it immediately.
Glad that it helped - sometimes it is the smallest thing that ruins our day.

How to use plurals correctly when using the sizeof function in php?

When the page display results, I want it to say say "We found X products" or "We found 1 Product" rather than "we found 1 products".
The relevant part of my current code is this:
<?php echo sizeof($ids); ?> Products</strong>
How could I extend it to behave more natural?
If count is more than one echo "s".
<?php echo sizeof($ids); ?> Product <?php If(count($ids)>1) echo "s"; ?></strong>
Or you make it easier to read like this:.
If(count($ids)>1) {
Echo sizeof($ids) . " Products.</strong>";
}Else{
Echo sizeof($ids) . " Product.</strong>";
}

How to pass a query string value to a page loaded via AJAX?

I am trying to put a variable into an AJAX-loaded page link I can call it on the content it loads?
I have tried:
<a href="#" onclick="ajaxpage('/content/staff/profile.php?nav={$array['username']}', 'content')">
<?php
$i++;
echo "<div style=\"float:left; width:{$row_width}%;text-align:center;\">\n";
echo "<i>»{$array['username']}</i>\n";
echo "</div>\n";
echo "</a><br />\n";
I tried putting the link inside and outside the php code but neither of them worked, I also tried a few differents ways of calling it including $array['username'] All I need is a way I can define the variable "nav" as username (which is set in a database) so I can use it on that content that is called by the link.
Is it even possible?
I am not sure I completely understood your question.
But perhaps you want to try this out:
<a href="#" onclick="ajaxpage('/content/staff/profile.php?nav=<?php echo $array['username'];?>', 'content')">
<?php
$i++;
echo "<div style=\"float:left; width:{$row_width}%;text-align:center;\">\n";
echo "<i>»", $array['username'], "</i>\n";
echo "</div>\n";
echo "</a><br />\n";

Show each date only once

All right, this must be an absolutely easy question, and I apologize for that.
I also apologize if I simply failed in finding the right search terms to use to come to an answer on my own. I did try, but my lack of fluency in PHP kind of makes me suck at searching.
I'm looking for a simple way to show each date only once within a foreach loop. I'm looping through data like so:
<?php
echo "<ul>";
foreach($rss_items as $i){
if($i->get_feed()->get_title() == 'Twitter (no # replies)'){
echo "<li>";
echo $i->get_date();
echo "<a href='" .$i->get_link()."'>Twitter</a>";
echo $i->get_title();
echo "</li>";
}
elseif($i->get_feed()->get_title() == 'Pinboard (jpcody)'){
echo "<li>";
echo $i->get_date();
echo "<a href='" .$i->get_link()."'>Pinboard</a>";
echo $i->get_title();
echo "</li>";
}
elseif($i->get_feed()->get_title() == 'Entries at Church Marketing Sucks by Joshua Cody'){
echo "<li>";
echo $i->get_date();
echo "<a href='" .$i->get_link()."'>Church Marketing Sucks</a>";
echo "<a href='" .$i->get_link()."'>" . $i->get_title() . "</a>";
echo $i->get_description();
echo "</li>";
}
elseif($i->get_feed()->get_title() == 'Flickr remove first paragraph'){
echo "<li>";
echo $i->get_date();
echo "<a href='" .$i->get_link()."'>Flickr</a>";
echo "<a href='" .$i->get_link()."'>" . $i->get_title() . "</a>";
echo $i->get_description();
echo "</li>";
}
}
echo "</ul>";
?>
And each item contains the date, so I'm getting the same date multiple times. I'd like to only have each date shown once, a la http://daringfireball.net.
I'm using CodeIgniter and the SimplePie library, so all of the data is being pulled directly instead of being stored in a db. I imagine a way to do it could be including a second if statement to check if the date has already been used, but I don't know how to execute this.
I'm pretty new to PHP, and I'm really looking to learn more than just have a solution given.
Any help you could give would be great!
You need to remember what was the date you used last, and print it only if it differs. You can try something like:
$previous_date = null;
foreach ($rss_items as $item) {
if ($item->get_date() != $previous_date) {
$previous_date = $item->get_date();
echo '<li>' . $previous_date . '</li>';
}
...
}
(And don't forget to HTML-encode the titles and links using htmlspecialchars.)
Do you mean you only want the date to be shown once, before the loop, or once per loop so that it looks like:
Date 1
- item 1
- item 2
- etc...
Date 2
- item 1
- item 2
- etc...
Could you clarify the format? Cause at the moment the date should be shown once for each $i that is valid.

Categories