PHP Recordset and EOF - php

I feel like I know this, but am getting frustrated that I can't remember exactly how to do this.
In PHP, I need to repeat items of a record set in a unordered list. I can repeat items fine, but I don't want anything to show if the record set is empty. Right now, if there is no record, the code is still displaying a blank list item, when I just want nothing to appear.
I have tried this:
<?php do { ?>
<li>Content Goes Here</li>
<?php } while (!feof($recordsetName) && $row_recordsetName = mysql_fetch_assoc($recordsetName)); ?>
And I have tried doing it this way by putting the repeating element withing an if/else statement like this:
<?php if (!feof($recordsetName)) {
echo ""; }
else do { ?>
<li>Content Goes Here</li>
<?php } while ($row_recordsetName = mysql_fetch_assoc($recordsetName));
; } ?>
But that isn't working either. Any information would be helpful

while($row = mysql_fetch_assoc($recordsetName)) {
if($row['fieldname'] != '') {
echo '<li>Content Goes Here</li>';
}
}
or you could check if the query was successful...
$result = mysql_query($sql);
if($result) {
echo '<li>Content Goes Here</li>';
}
or if it returned any results...
$results = mysql_fetch_assoc($recordsetName);
if(mysql_num_rows($results) > 0) {
while($row = mysql_fetch_assoc($results)) {
echo '<li>Content Goes Here</li>';
}
}
Also, you should really be using mysqli or PDO, as mysql is depreciated.
Edit: added loop in example 3.

Related

Removing last comma from a anchor link?

I'm currently working on a code that retrieves the first names and last names from a database and adds a link to other pages using an anchor link. This is the code:
<?php
$get_names_query_result = $get_names_query->get_result();
if (!$get_names_query_result) {
xecho("Names query failed.");
} else {
while ($name = $get_names_query_result->fetch_assoc()) {
?>
<?php xecho($name['first_name']) ?> <?php xecho($name['last_name']) ?>,
<?php
} // while fetch_assoc()
} // if $get_names_query_result
This code displays this on the webpage:
code-commas
I would like to eliminate that trailing comma at the end of the last element but I'm pretty lost on how to do it only for the name shown.
Any help would be much appreciated!
One approach I use is to create a variable, say $comma, and initialise it as "". Then after its first use, which adds nothing to your output, change it to ",". SO...
<?php
$comma = "";
$get_names_query_result = $get_names_query->get_result();
if (!$get_names_query_result) {
xecho("Names query failed.");
} else {
while ($name = $get_names_query_result->fetch_assoc()) {
?>
<?php echo $comma; ?> <?php xecho($name['first_name']) ?> <?php xecho($name['last_name']) ?>
<?php
$comma = ","
} // while fetch_assoc()
} // if $get_names_query_result
Note that the comma is now added before the anchor, not after it as you had it.

PHP - Unable to get same number of </ul> as <ul>

I am unable to generate same number of </ul> as <ul> in the following code:
<?php
$array=array("item1","item2","item3");
for ($i=0;$i<count($array);$i++)
{
if($i<count($array)-1)
{
echo '<li><span>'.$array[$i].'</span><ul>';
}
else
{
echo '<li><span>'.$array[$i].'</span>';
}
}
for ($i=0;$i<count($array);$i++)
{
echo '</ul></li>';
}
?>
The code is generating uneven number of <li><ul> and </ul></li>. Please tell me where I am going wrong?
This is a minor error. In your second for statement change $i<count($array); to $i<count($array)-1;.
So your final code will be:
<?php
$array=array("item1","item2","item3");
for ($i=0;$i<count($array);$i++)
{
if($i<count($array)-1)
{
echo '<li><span>'.$array[$i].'</span><ul>';
}
else
{
echo '<li><span>'.$array[$i].'</span>';
}
}
for ($i=0;$i<count($array)-1;$i++)
{
echo '</ul></li>';
}
?>
With if($i<count($array)-1) you check if $i is smaller than the length - 1.
That check isn't performed in the second loop. So the first one actually performs as many steps as the second one but only writes in all but the last iteration.
Try This:
$array=array("item1","item2","item3");
echo '<ul>';
for ($i=0;$i<count($array);$i++)
{
echo '<li><span>'.$array[$i].'</span></li>';
}
echo '</ul>;

If statement within echo?

I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;

more button need to show after some count

if(count($fetchExperiences) > 0)
{
$i=0;
foreach($fetchExperiences as $fetchExperience)
{
$sqlExperiencedat = $db->createCommand(" code ");
$fetchExperiencesdat = $sqlExperiencedat->queryAll();
$tdyyy=date('YYYY-mm-dd');
if(strtotime($tdyyy)<strtotime($fetchExperience['experience_date']))
{
foreach($fetchExperiencesdat as $fetchExperiencesdatt)
{
if(strtotime($tdyyy)<strtotime($fetchExperiencesdatt['avail_date']))
{
//echo $fetchExperiencesdatt['avail_date'];
$tth=1;
}
}
if($tth==1){
//fetchExperiencesdatt code
}
}
}
if(count($fetchCountExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}
}
}
My question is that I want more button after $fetchexperiences count is greater than 8. It is fine. But in between $fetchexperiences loop there is a loop for $fetchExperiencesdat. by the loop if there is no experince with in availdate it is not shown. so the probelm is more button is display not after 8 because of some $fetchExperiencesdat elements should be hidden. So please give me any solution for this.
Thanking you.
I'm not sure I understand where your count experiences are coming from. However, the problem your code has is that fetchCountExperiences is not given a value.
if(count($fetchCountExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}
Looks like it should be
if(count($fetchExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}

PHP function that decides which HTML code to use?

I'm looking for a function like and if else statement for php which will execute certain html code.
For example:
<?php>
$result = 1;
if ($result == 1)
<?>
html code
else
html code
So, based off the result variable gotten from php scripts, a certain html page is output. I've tried echoing the entire html page, but it just displays the html code-> tags and such.
Hopefully you get what I'm trying to get across, ask if you need any clarification questions. Thanks!
That should work:
<?php
$result = 1;
if($result==1) {
?>
html code
<?php
} else {
?>
html code
<?php
}
?>
The problem I'm facing with the if else statement, is in order to display the html, I have to exit php coding. Thus, the if else statement will not work. (Link)
This is not entirely true. You can use the approach below:
<?php
// Do evaluations
if ( $result == "something" )
{
?>
<p>Example HTML code</p>
<?php
} elseif ( $result == "another thing")
{
?>
<span>Different HTML code</p>
<?php
} else {
?>
<h4>Foobar.</h4>
<?php
}
// Rest of the PHP code
?>
Or, if you don't want to exit PHP coding, you can use the echo or print statements. Example:
<?php
// Evaluations
if ( $result == "foo" )
{
echo "<p>Bar.</p>";
} else {
echo "<h4>Baz</p>";
}
// Some else PHP code
?>
Just be careful with proper sequences of ' and " characters. If your HTML tags are to have arguments, you should watch your step and use either of the following approaches:
echo "<span class=\"foo\">bar</span>";
echo '<span class="foo">bar</span>";
If you want to evaluate some PHP and print the HTML results later, you could use something like this
<?php
$output = "";
if ( $result == "something" ) {
$output = '<p>Example HTML code</p>';
} else if ( $result == "another thing") {
$output = '<span>Different HTML code</p>';
} else {
$output = '<h4>Foobar.</h4>';
}
// Output wherever you like
echo $output;
?>
EDIT (because I'm not sure what you;re trying to do so i'm just putting out different ideas):
If you're trying to output an entire page, it may be useful to use header('location: newPage.html'); instead of $output. This redirects the browser to an entirely new web page. Or you can likely include newPage.html as well.
very close:
<?php
$result = 1;
if ($result == 1){
?>
html code
<?php } //close if
else {
?>
html code
<?php
} //close else
?>
you can echo html code something like this
<?php
$result = 1;
if ($result == 1){
echo "<h1>I love using PHP!</h1>";
}
?>
this would output if Result is 1
**I love using PHP!** //but slightly bigger since its H1

Categories