Reputation: 525
I'm following through a tutorial from a book I have but want to add some extra columns to a table. I have added the columns, buy and sell, and in each of those I want to display a button. I am unsure of how to do this, is it possible?
Here's my code from the page with the table:
<?php // Example 21-9: members.php
include_once 'header.php';
if (!$loggedin) die();
echo "<div class='main'>";
$con=mysqli_connect("localhost","root","usbw","stocktrading");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM stocks");
echo "<table border='1 '>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Buy</th>
<th>Sell</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Upvotes: 3
Views: 19765
Reputation: 6527
I know that you must be a new programmer, but there's some cool things that you can use for avoid string concatenation. String concatenation sometimes can make your code messy and unreadable, and it's not cool.
You can use HEREDOC
for avoid concatenation(please, avoid concatenation). Also, when using HEREDOC
or double quotes "
you can use {}
to access array keys or object attributes.
i.e with HEREDOC:
// Guys, look, it's a HEREDOC, it make the HTML more readable :)
$html = <<<EOF
<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['price']}</td>
<td><button>Sell</button><td>
<td><button>Buy</button><td>
</tr>
EOF;
i.e with double quotes "
:
$html = "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['price']}</td>
<td><button>Sell</button><td>
<td><button>Buy</button><td>
</tr>";
But, if i need to call some functions?
sprintf
or printf
can be the solution
spritnf
: returns a string produced according to the formatting string format.
printf
: prints a string produced according to the formatting string format.
i.e:
$str = sprintf("My name is <b>%s</b>", ucfirst("i am not procrastinating"));
echo $str;
//OR
printf("My name is <b>%s</b>", ucfirst("i am not procrastinating"));
Or using an template way(may be hard) using str_replace
, array_keys
and array_values
.
$template = "My name is <b>:name:</b>, i'm from :from:.";
$templateVars = array(
":name:" => "I am not procrastinating",
":from:" => "Brazil"
);
echo str_replace(array_keys($templateVars),array_values($templateVars),$template);
Happy Coding.
Sorry for the English, but i'm Brazilian, and we don't speak English, not even Spanish haha.
Upvotes: 3
Reputation: 13586
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td><input type='radio' name='buysell' value='buy'></td>";
echo "<td><input type='radio' name='buysell' value='sell'></td>";
echo "</tr>";
Something like this will add radio buttons. Use checkboxes or another kind of button if you prefer.
Upvotes: 5
Reputation: 494
just add the button inside the td
echo "<tr>".
"<td>" . $row['id'] . "</td>" .
"<td>" . $row['name'] . "</td>" .
"<td>" . $row['price'] . "</td>" .
'<td><button>Sell</button><td>' .
'<td><button>Buy</button><td>' .
"</tr>";
Upvotes: 1