Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am so new to PHP, now i am trying to print the not null values. I have following php code which throws me all values including null and not nulls. In my website I need only not null values.
<tr>
<td><?php echo $stock['stsymbol']?><td>
<td><?php echo $stock['noshares']?><td>
<td><?php echo $stock['purchaseprice']?><td>
<td><?php echo $stock['datepurchased']?><td>
<td><?php echo $stock['Original Value']?><td>
<td><?php echo $stock['Current Price']?><td>
<td><?php echo $stock['Current Value']?><td>
</tr>
<?php } // end foreach ?>
change the all td like this example :
<?php if(!is_null($stock['stsymbol'])){ ?>
<td><?php echo $stock['stsymbol']; ?><td>
<?php } ?>
try using this
<td><?php if(!empty($stock['stsymbol'])){ echo $stock['stsymbol']; }?><td>
php has is_null clearly for situation like this. use
if (!is_null($varname)) echo $varname;
The basic pattern you want to follow to conditionally print a value is
<?php
if (!empty($stock['stsymbol'])) {
echo $stock['stsymbol'];
}
?>
This can be shortened by using a ternary if
<?php echo (!empty($stock["stsymbol"]))?$stock["stsymbol"]:"" ?>
UPDATE
Much discussion has gone on in the comments regarding the mechanism used to test for a null value. I thought it might be good to recap the merits of each.
is_null - returns true if the variable is strictly null or undefined (will issue a notice if it is undefined)
empty - returns true if the variable is falsy or undefined (will not issue a notice)
!$val - same as empty but with a notice with undefined values.
You can avoid the problem altogether and use another foreach loop in your current foreach.
It goes like this:
foreach ($stock as $key=>$value) {
echo "<td>$value</td>";
}
Related
This question already has answers here:
How to pass multiple parameters in a querystring
(7 answers)
Closed 1 year ago.
I'm carrying one variable in "reserva" wich I later I use $_GET('reserva'), but I also need another one of the rows,how could I do it?
<?php
while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['idps'];?></td>
<td><?php echo $row['Descrição'];?></td>
<td><?php echo $row['dia'];?></td>
<td><?php echo $row['dataini'];?></td>
<td><?php echo $row['datafim'];?></td>
<td><?php echo $row['tblstaff_idPrestador'];?></td>
<td><a href='marcar.php?reserva=<?php echo $row['idps'];?>'>Marcar</a></td>
</tr>
<?php endwhile;?>
Yes, you can append multiple values in a GET request using &:
<a href='marcar.php?reserva=<?php echo $row['idps'];?>&key=value'>Marcar</a>
^^^^^^^^^^
as #raina77ow pointed out, be aware that & is a "reserved" char, so if your string contains that, you have to encode it, and for that, in PHP you have the function urlencode
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a foreach loop but i want level only print once like example below.
https://prnt.sc/wgm877
But when i try foreach loop it print again and again.
https://prnt.sc/wgmazh
<?php foreach($rooms as $room) { ?>
<tr>
<td><?php echo $room->Level; ?></td>
<td><?php echo $room->Type; ?></td>
<td><?php echo $room->Dimension; ?></td>
</tr>
<?php } ?>
I would solve it like this. I.e. "remember" the last room level for each iteration. If it's changed, then print the new room level, otherwise print an empty string:
<?php
$lastlevel = "";
foreach($rooms as $room) {
if ($lastlevel != $room->Level) {
$level = $room->Level;
$lastlevel = $level;
} else {
$level = " ";
}
$nextrow = <<<EOD
<tr>
<td>{$level}</td>
<td>{$room->Type}</td>
<td>{$room->Dimension}</td>
</tr>
EOD;
echo $nextrow;
}
?>
You can either change the structure and do a foreach on the levels instead of the rooms, or use a new variable to know if the level has already been displayed
<? foreach ($actions as $action): ?>
<option value="<?= $action ?>"><?= $action ?>
<? endforeach; ?>
If you want to implement php on the frontend with html, I recommend doing it in the following way.
This will in this case echo multiple authors, release dates, and covers for a book on a single page.
My question is: How do I make it look nice with a table, and my CSS file? I just can't get it right, kinda almost made it work once, but it showed just the one book and then started on a new row, and I want at least 4 books/row, and the borders were completely off.
Really silly question I know, and I've tried googling and experimenting but I'm having serious trouble making it really work.
Also, if I want to echo a variable ($genre) in a link-text, say
Home > Books > CurrentGenreVariable($genre)
how'd I do that neatly? I need a slight kick-start, that's all I think.
Thanks in advance.
You follow the generic looping patterns. I presume you're doing this with a while() loop?
<table>
<?php while ($row = FETCH_THE_DATA): ?>
<tr>
<td><?php echo $row['author']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['cover']; ?></td>
</tr>
<?php endwhile; ?>
</table>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I need some little help on a simple thing!
I have this table in my database, where I have a number of images stored, called "covers".
And then I have a table in HTML that displays the covers of this MySQL table inside one of it's cells.
Like this:
<td><a href="movie.php?id=<?php echo $idt + 1; ?>">
<?php echo '<img src="'.htmlentities($idt + 1['cover'], ENT_QUOTES, 'UTF-8').'" alt="Cover" style="max-width:300px;max-height:300px;" />';; ?></a></td>
And this is the code before
$req = mysql_query("select id, name, year, genre, cover from movies");
$dnn = mysql_fetch_array($req);
$idt = $dnn['id'];
But why doesn't it work when I try to dynamically change id by putting this?
$idt + 1;
In order to output data from multiple rows, I suggest using a loop like this:
while ($row = mysql_fetch_assoc($req)) {
?><td>
<a href="movie.php?id=<?php echo $row['id']; ?>">
<img src="<?php echo htmlentities($row['cover'], ENT_QUOTES, 'UTF-8'); ?>" alt="Cover" />
</a>
</td><?php
}
You need to fetch a whole set of results and not just one id and increment it in the view. You should do a foreach loop on $idt like that:
foreach($dnn as $row){
$id=$row['id'];
$cover=$row['cover'];
$genre=$row['genre'];
//etc...
//now echo html with vars like this:
echo "<img src=\"$cover\"/>";
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
It's been a long time since I have done php so sorry for the silly question.
This is my current code, I'm trying to save the URL as a variable so that I can insert it into the echo, but it doesn't seem to work as nothing appears:
<?php ob_start();
echo get_post_meta($post->ID, 'oldurl', true);
$old_url = ob_get_contents();
ob_end_clean();
?>
<?php echo do_shortcode('[fbcomments][fbcomments url="$old_url" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
I have echoed $old_url and can see that it has the correct value, but how do I insert the value into the echo do_shortcode with url="$old_url"?
This doesn't work either:
<?php echo do_shortcode('[fbcomments][fbcomments url="echo $old_url;" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
You'll need to switch your quotes around. Single quotes print everything out as-is. Double-quotes will process the variables. Also, echo is not needed within an echo.
<?php echo do_shortcode("[fbcomments][fbcomments url='$old_url' width='375' count='off' num='3' countmsg='wonderful comments!']"); ?>
Another way to do it without switching your quotes is to break out of the statement:
<?php echo do_shortcode('[fbcomments][fbcomments url="'.$old_url.'" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Variables are not replaced in single quotes ...
<?php echo do_shortcode('[fbcomments][fbcomments url="' . $old_url . '" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Singles quotes doesn't allow variables parsing.
For example :
$var = 'Hello';
echo 'The content of my var is : $var';
// Will output : "The content of my var is : $var"
echo "The content of my var is : $var";
// Will output : "The content of my var is : Hello"
So you have to use double quotes or use the concatenate operator : .