This question already has answers here:
How do I access this object property with an illegal name?
(2 answers)
Closed 9 years ago.
So I'm writing a php project and I got stuck on hyphens in column names of a table in a database.
I have the following code:
$results = $conn->query($sql)->fetchAll(PDO::FETCH_OBJ);
foreach ($results as $row) { ?>
<tr>
<td><?= $row->factuur-status ?></td>
<td><?= $row->verkoop-orderid ?></td>
</tr>
<?php } ?>
Now obviously because of the hyphens in the column names this doesn't work.
How can one fix this?
I found it, if someone's interested:
<?= $row->{'factuur-status'} ?>
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
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 2 years ago.
Inside the body of an html document I have something like this:
<?=str_replace(' ', '_', $result[0]['something'])?>
This works perfectly fine. In the same document I have this:
<?php if(!empty($result[0]['something'])) { echo "Hello"; } else { echo " "; }?>
Which also works fine, but it slightly bothers me that I am using <?= in one place and <?php in another. When I try to change the if code to become:
<?=if(!empty($result[0]['something'])) { echo "Hello"; } else { echo " "; }?>
or
<?= if(!empty($result[0]['something'])) { echo "Hello"; } else { echo " "; }?>
Both result in a Parse error: syntax error, unexpected 'if' (T_IF) in....
I've attempted to find some documentation on the respective differences between <?php and <?= as a php opening tag but all I get is data on short tags - which this is not. Can someone explain this behavior for me?
<?= is like <?php echo. You can't echo an if statement.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 2 years ago.
Hey below I tried to display my image, it may be from the way I referenced in the src. Any ideas hot to fix? Thanks
(I get Parse error: syntax error, unexpected '<' )
<td><?php echo <img src='"/images/thumbs/.$row['thumb']."';?></td>
more ways... just keep attention on " and ' orders
<td><?php echo '<img src="/images/thumbs/'.$row['thumb'].'"';?></td>
you may also use:
<td><?php echo "<img src=\"/images/thumbs/{$row['thumb']}\"";?></td>
hope to be usefull
A cleaner method:
<?php $foo = $row['thumb'];?>
<td><?php echo '<img src="/images/thumbs/'.$foo.'"';?></td>
This question already has answers here:
PHP Dynamic Variable Name
(2 answers)
Closed 8 years ago.
I have a for script:
<?php for($i=1; $i<10; $i++):?>
<?php if($this->item->nuotrauka{$i}):?>
<div class="vobsmall">
<img src="<?php echo $this->item->nuotrauka{$i}; ?>"/>
<?php echo $this->item->nuotrauka{$i}; ?>
</div>
<?php endif; ?>
<?php endfor; ?>
There's 9 variables like $this->item->nuotrauka1, $this->item->nuotrauka2 etc.
As you can see here I'm trying to call these variables using {i} as number of object variable. Hovewer the code above returns single char from variable and not full variable as I want. How should I write {i} here to get what I want ?
Try this:
echo $this->item->{"nuotrauka".$i};
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>";
}