Pls what am I doing wrong?
I have an array ($result) having some keys and values in it obtained via doctrine_mongodb and I am trying to display the results in a table using php in different cells.
foreach ($result as $docrow)
{
echo "<tr height=\"20\" class=\"evenListRowS1\">";
**echo "<td>"$docrow->getName()"</td>";**
echo "<td>1</td>";
echo "<td>2</td>";
Seems like the issue is with the "" (exclamation marks) when I use . Error message i get is:
Parse: syntax error, unexpected '$docrow' (T_VARIABLE), expecting ',' or ';'...
Use dot(.) to concat echo try this
foreach ($result as $docrow)
{
echo "<tr height=\"20\" class=\"evenListRowS1\">";
echo "<td>".$docrow->getName()."</td>";
echo "<td>1</td>";
echo "<td>2</td>";
Try this, You have missed to add . concat the variable between the strings.
echo "<td>".$docrow->getName()."</td>";
.....^
instead of
echo "<td>"$docrow->getName()"</td>";
Now your code should be,
foreach ($result as $docrow)
{
echo "<tr height=\"20\" class=\"evenListRowS1\">";
echo "<td>".$docrow->getName()."</td>";
echo "<td>1</td>";
echo "<td>2</td>";
}
echo "<td>$docrow->getName()</td>";
You don't need to unquote to use a variable if using double quotes.
If you left your code the way it was, you would either need to a) put a comma after the "" and one after $docrow->getName(), or b) concatenate the variable into the string.
1. You missed dot before and after $docrow->getName()
Try :
echo "<td>".$docrow->getName()."</td>";
Or :
echo "<td>{$docrow->getName()}</td>";
Read this for more info : String operators
2 . And you missed a bracket after your loop :
foreach ($result as $docrow)
{
echo "<tr height=\"20\" class=\"evenListRowS1\">";
echo "<td>"$docrow->getName()"</td>";
echo "<td>1</td>";
echo "<td>2</td>";
} // <-- you missed this
Related
I am trying to get my code to display in an HTML table using the while loop. However, I can't get my table to display. Is there something wrong with the way I am trying to echo the table out?
<?php
#-----call includes-----# include('E:/includes/database.php');
include('E:/includes/sendmail.php');
ini_set('error_reporting', E_ALL ^ E_NOTICE);
ini_set('display_errors','on');
$do = $_REQUEST['do'];
$queryOrders = "select t2.SlpName As 'Seller', t1.SWW As 'Vendor', t0.DocDate As 'Date',t0.DocTotal As 'Total'
from twh.dbo.OINV t0
inner join twh.dbo.inv1 t1 on t0.DocEntry = t1.DocEntry
inner join twh.dbo.OSLP t2 on t1.SlpCode = t2.SlpCode
where t0.DocDate > DATEADD (month, -2, getdate())
order by t0.DocTotal desc";
$resultItemData = sqlsrv_query($con, $queryOrders);
echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Seller</th>";
echo "<th>Vendor</th>";
echo "<th>Date</th>";
echo "<th>Total</th></tr>"
while($rowItemData = sqlsrv_fetch_array($resultItemData)){
echo "<tr><td>";
echo "$resultItemData";
echo "</td></tr>";
endwhile;
}
echo"</table>";
Modified a little bit. Check this:
while($rowItemData = sqlsrv_fetch_array($resultItemData)) :
echo "<tr><td>";
echo $rowItemData[ColumnValue]; //In your code, didn't access the column value earlier
echo "</td></tr>";
endwhile;
There's a combination of problems at play:
First, you open the while with {, but close with endwhile; - while technically not a problem, it's not consistent - if you open with {, it's best practice to close with }.
Second, you're attempting to echo an entire array, which won't work properly.
Third, no need to put the value inside of quotes: echo "$resultItemData"; could simply be echo $resultItemData.
Fourth, you're attempting to echo $resultItemData, which is a resource, not the row data. You want to echo $rowItemData values.
And finally, you'll likely want the results in an associative array, rather than a numerically-indexed array, so you might consider using sqlsrv_fetch_array( $resultItemData, SQLSRV_FETCH_ASSOC).
Below is your code, revised to work, and follow a bit better practices:
// columns: 'Seller',Vendor, 'Date', 'Total'
while( $rowItemData = sqlsrv_fetch_array( $resultItemData, SQLSRV_FETCH_ASSOC ) ) {
echo "<tr><td>";
echo "<td>$rowItemData['Seller']</td>";
echo "<td>$rowItemData['Vendor']</td>";
echo "<td>$rowItemData['Date']</td>";
echo "<td>$rowItemData['Total']</td>";
echo "</tr>";
}
You should specify the indexes of the array $rowitemdata.
For example:
echo"<tr><td>".$rowitemdata['Vendor']."
I haven't gone through your full code, but I can see wrong syntax for while
while($rowItemData = sqlsrv_fetch_array($resultItemData)){
echo "<tr>";
echo "<td>".$resultItemData['Seller']."</td>";
echo "<td>".$resultItemData['Vendor']."</td>";
echo "<td>".$resultItemData['Date']."</td>";
echo "<td>".$resultItemData['Total']."</td>";
echo "</tr>";
}
or
while($rowItemData = sqlsrv_fetch_array($resultItemData)) :
echo "<tr>";
echo "<td>".$resultItemData['Seller']."</td>";
echo "<td>".$resultItemData['Vendor']."</td>";
echo "<td>".$resultItemData['Date']."</td>";
echo "<td>".$resultItemData['Total']."</td>";
echo "</tr>";
endwhile;
Update:
still you can optimize the code, I just gave sample working
Let's assume your query actually produces a result, because you don't check that it does:
$resultItemData = sqlsrv_query($con, $queryOrders);
$header = <<<HEREDOC
<table border="1" align="center">
<tr>
<th>Seller</th>
<th>Vendor</th>
<th>Date</th>
<th>Total</th>
</tr>
HEREDOC;
echo $header;
while($rowItemData = sqlsrv_fetch_array($resultItemData)) {
echo "<tr>
<td>{$rowItemData['Seller']}</td>
<td>{$rowItemData['Vendor']}</td>
<td>{$rowItemData['Date']}</td>
<td>{$rowItemData['Total']}</td>
</tr>";
}
echo '</table>';
To actually check that the query works, you might want to do this instead. This is just to debug/illustrate error checking for the query execution. You wouldn't want to output an error to the screen, but rather log it. You probably want to just output the table header/footer and skip the while/loop entirely.
$resultItemData = sqlsrv_query($con, $queryOrders);
if (resultItemData === false) {
die(print_r(sqlsrv_errors(), true));
}
looking for a solution to this bit of coding below.
<?php
$nextfive_events = mysql_query("SELECT date_format(date, '%d/%m/%Y') AS formatted_date, title, location, regs FROM events WHERE status = 'ACTIVE'");
if(mysql_num_rows($nextfive_events) == 0) { echo "<p>No events coming up!"; } else {
echo "<table width=\"600\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"greywritinglight\" align=\"left\">
<tr align=\"center\">
<td>Date</td>
<td>Name</td>
<td>Location</td>
<td></td>
</tr>";
$x=1;
while($next_row = mysql_fetch_array($nextfive_events))
{
if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif;
echo "<tr align=\"center\">";
echo "<td>" . $next_row['formatted_date'] . "</td>";
echo "<td>" . $next_row['title'] . "</td>";
echo "<td>" . $next_row['location'] . "</td>";
echo "<td>Regs</td>";
echo "</tr>";
$x++;
}
echo "</table>";
}
?>
I want the row echo "<td> <a href regs .....
To display the word Regs when there is something in 'regs' in the database. Say if there is nothing in that field I want it to be blank and not say Regs.
thanks
You could do Ternary operator:
echo "<td><a href='" . (empty($next_row['regs']) ? "#" : $next_row['regs']) . "'>Regs</a></td>";
Try not to do escape characters, they look confusing, do single quote for href attribute. Also, Did you want
<a href='#'>Regs</a>
to show if it was blank?
If Not, try this:
echo (!empty($next_row['regs']) ? "<td><a href='" . $next_row['regs'] . "'>Regs</a></td>" : "");
You could use a ternary:
echo ( ! empty($next_row['regs'])) ? '<td>Regs</td>' : '<td>&nspb;</td>';
First off, I'd like to show you a really handy trick with PHP that allows you to echo out into HTML without actually saying echo. Just create a break in your PHP code, and in between anything you put will be echoed as HTML.
As for the number of rows returned, you are doing it correctly. Make sure that you are querying properly, that you have an established connection, and that there are no logical errors in your SQL.
Sorry, didn't notice you wanted to check if the column was empty! Just use the function empty(). This function will return true when the data you give it is empty, so simply give it the column from the row of data you wish to check.
<?php
// Lets say this is your database connection variable
$connection = mysqli_connect(//Your info goes here);
// This is the query you want to run
$query = "SELECT something FROM somewhere WHERE something='$something_else'";
// This is where you are storing your results of the query
$data = mysqli_query($connection, $query);
// Here, you can ask for how many rows were returned
// In PHP, ZERO is evaluated to false. We can use a
// Short-hand boolean expression like this
if (mysqli_num_rows($data))
{
// Because zero evaluates to false, we know
// that if we get HERE that there ARE rows
// We can break out of PHP and into our HTML
// by simply ending our PHP tag! Just remember
// that you need to open it back up again though!
?>
<!--You can put your HTML here-->
<p>
We found some rows that you might
need to know about!
</p>
<?php
}
else
{
// But, if we get here, we know it evaluated as
// FALSE and therefore no rows returned
// Here's that HTML trick again
?>
<!--HTML can go here-->
<p>
No rows returned!
</p>
<?php
}
?>
I have changed my code and now I have a new error:
Parse error: syntax error, unexpected '<' in
/webroot/c/o/..../...../www/a/drop_down_car_test.php on line 19
here is the code:
<?php
//////connecting to our sql server
$db_connect=mysql_connect("xxxx", "xxxxxxxxx", "xxxxxxxxxx") or die("not logged in");
//////now selecting our database
$db_select=mysql_select_db("coden003_car") or die(mysql_error());
////this query is used the first time the page loads
$query = mysql_query("SELECT * FROM car ");
echo '<form action="drop_down_car_test.php?" method="GET">';
echo '<table border = \"1\">';
echo '<tr><td>id</td><td>car</td><td>name</td>';
while($row=mysql_fetch_array($query)){
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo "<select name='carDropDown." . $row['id'] . "' >";
<option value="1">ford</option>; <---------line 19
<option value="2">toyota</option>;
<option value="3">gmc</option>;
</select>";
echo "</td><td>";
echo $row['name'];
echo "</td><td>";
}////end while
echo"<table>";
echo '<td bgcolor=#7FFF00><input type="submit" value="Update"></td>';
echo "</form>";
?>
im use to C++ usage of quotes and PHP confuses me.
In your code:
echo '<select name="carDropDown.$row['id']">
You need to make it:
echo "<select name=\"carDropDown.{$row['id']}\">
You were not escaping the ', but also you can't use a variable inside single quotes, but you can use one inside double quotes (array calls must be wrapped in { and }).
And of course the closing ' at the end of that string needs to be changed to "
And you're missing a semicolon at the end of that string.
And you didn't close the block (you forgot a closing }).
Try these:
echo "<select name='carDropDown." . $row['id'] . "' >";
OR
echo "<select name=carDropDown.$row['id'] >";
In PHP you can CONCAT strings like this:
echo "hello" . "world" . $yourVariable . " Something else ";
http://php.net/manual/en/internals2.opcodes.concat.php
Another way to do the <select> name, which will make it a little easier to read on the $_GET[] is to make carDropDown an array with the $row['id'] as the array key.
echo '<select name="carDropDown['.$row['id'].']">
Now on the php side you can do -
foreach ($row['id'] as $id){
$carName=$_GET['carDropDown'][$id]];
echo $carName;}
I've written a php code to display all the images. But there's is something wrong in the code and I can't fix it. It's kind of a syntax error but I've wasted hours over it and still mixing up the "quotes(')"..here's my php code:
while($row = mysql_fetch_array($display_query)){
print "<tr><td>".$row['itemid']."</td><td><img src="resources/wh/'.$row['itemid'].'.png"/></td><td>".$row['description']."</td><td>";
print "₹".$row['cost']."</td></tr>";
}
"<tr><td>".$row['itemid']."</td><td><img src="resources/wh/'.$row['itemid'].'.png"/></td><td>".$row['description']."</td><td>";
Should be
"<tr><td>".$row['itemid'] . '</td><td><img src="resources/wh/'.$row['itemid'].'.png"/></td><td>'.$row['description']."</td><td>";
But mix ' and " make your code a mess, you could use HEREDOC to make it more readable.
while($row = mysql_fetch_array($display_query)){
echo <<<EOF
<tr>
<td>{$row['itemid']}</td>
<td><img src="resources/wh/{$row['itemid']}.png"/></td>
<td>{$row['description']}</td>
<td>₹{$row['cost']}</td>
</tr>
EOF;
}
Your concatenation of string and quotation is not right. Try this -
while($row = mysql_fetch_array($display_query)){
print "<tr><td>" . $row['itemid']."</td><td><img src=" . 'resources/wh/' .$row['itemid']. ".png'/></td><td>".$row['description'] . "</td><td>";
print "₹".$row['cost']."</td></tr>";
}
The simplest solution is just make proper pencuation in your code.
The correct code is:
print "<tr><td>".$row['itemid']."</td><td><img src='resources/wh/".$row['itemid'].".png'/></td><td>".$row['description']."</td><td>";
from db we get column value of rating $number=$row->rating ;
if we want TO print image as rating value in a table row then
then
$number=$row->rating ;
$middle="";
$first="<td width='200' align='left'>";
for($x=1;$x<=$number;$x++) {
$middle=$middle.img($fullimage_properties);
}
if (strpos($number,'.')) {
$middle=$middle.img($halfimage_properties);
$x++;
}
while ($x<=5) {
$middle=$middle.img($blankimage_properties); ;
$x++;
}
echo $last=$first.$middle."</td>";
I am trying to output a table using php variables like this:
echo "<tr>";
echo "<td>".$var1."</td>";
echo "<td>".$var2."</td>";
echo "</tr>";
It works fine if both variables exist.. but if $var1 is null, the $var2 value is outputted to the firs column in the HTML table.
Is there a way to properly output the columns by naming them?
P.S: I can't do "if $var == NULL ...." for some coding reasons. I want to control the HTML output if possible.
While I agree with #leeb above, do you want something like this:
echo "<td>" . isset($var1)?$var1:"" . "</td>";
echo "<tr>";
echo "<td>".$var1." </td>";
echo "<td>".$var2." </td>";
echo "</tr>";