I have mysql table with following columns :
no, name, oname, quantity
I have n number of records in the tables ,I want to fetch these records in an html table that dynamically increases'decreases its row length according to the number of rows in mysql database. I am trying following but its not working.can anyone help me out here
<?php
include './connection.php';
$query = "select * from orderdetails";
$result = mysql_query($query);
echo '<table border="1" style="width:600px" align=center >';
echo '<tr bgcolor="lavendar">';
echo '<td width="15%">Order No.</td>';
echo '<td>Name</td>';
echo '<td>Order</td>';
echo '<td>Quantity</td>';
echo '</tr>';
echo '</table>';
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
echo '</table>';
?>
Where you are printing out your values, there are a few errors.
In this specific code block:
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
First off all, you would want to concatinate your strings, PHP uses . for that.
For example, to concatinate "World!" to "Hello, ", to print out Hello, World!, you would use the following:
echo "Hello, " . "World!";
You are also missing a variable sign ($) in your code, when printing out the rows.
In your case, you're using echo '<td>' row['no'] '</td>';,
where it should have been echo '<td>' . $row['no'] . '</td>';
To clarify, your code should look like this:
while($row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' . $row['no'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['oname'] . '</td>';
echo '<td>' . $row['quantity'] . '</td>';
echo '</tr>';
}
Relevant PHP documentation:
PHP String Concatination
Echo documentation
There's a typo-
row['no']
to
$row['no']
Similarly other variables too.
Related
This code works but the one belows shows an error
if(isset($_POST['month'])=='')
{
$sql = ('SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM 2017_oct_land
WHERE taxability= "T"
group by District ASC');
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
--with AND-- why do i get this "Warning: Invalid argument supplied for foreach() in C:\wamp64\www\reportview\pages\code2.php on line 20" with this code?
if(isset($_POST['month'])!=NULL)
{
$kind = $_POST['kind'];
echo "MONTH : ".$kind."<br/>";
$sql = ('SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM julcons
WHERE taxability="T" and actualuse like "' . $kind .'"');
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
Here you go ( untested ) but should be close
if(isset($_POST['kind'])) {
//why check month and then use kind? which is it..
//isset returns boolean, not null, null works because it's false but it's not correct IMO
$kind = $_POST['kind']; //what is this kind or month
echo "MONTH : ".$kind."<br/>";
$sql = 'SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM julcons
WHERE taxability="T" and actualuse like :kind';
$stmt = $pdo->prepare($sql);
$stmt->execute([':kind' => $kind]);
while( false !== ( $row = $stmt->fetch(PDO::FETCH_ASSOC))){
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
Sorry, but you had some really big mistakes in your code.
I have a code where I echo a table:
<?php
// connect to the database
include('core/base.php');
// get results from database
$result = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM uitslag ORDER BY ID ASC")
or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Nummer</th><th>Naam</th><th>Telefoon</th><th>Binnen</th> <th>Adres</th> <th>Postcode</th> <th>Wijk</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Naam'] . '</td>';
echo '<td>' . $row['Telefoon'] . '</td>';
echo '<td>' . $row['Binnen'] . '</td>';
echo '<td>' . $row['Adres'] . '</td>';
echo '<td>' . $row['Postcode'] . '</td>';
echo '<td>' . $row['Wijk'] . '</td>';
echo '<td>Aanpassen</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
The row 'Telefoon' either echos 1 or 0. How can I echo a checkbox which is checked when 1 instead of echoing the actual number?
Define input element of type checkbox.Like this.
$telefoon = ($row['Telefoon']==1)?'checked':'';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Naam'] . '</td>';
echo "<td><input type='checkbox' $telefoon></td>";
echo '<td>' . $row['Binnen'] . '</td>';
echo '<td>' . $row['Adres'] . '</td>';
echo '<td>' . $row['Postcode'] . '</td>';
echo '<td>' . $row['Wijk'] . '</td>';
<?php
if($row['Telefoon']==1){
echo "<td><input type='checkbox' checked></td>";
}
else {
echo "<td><input type='checkbox' ></td>";
}
?>
I am creating a table by echoing results out as the following:
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM kayitlar ORDER BY id DESC';
foreach ($pdo->query($sql) as $row)
{
echo '<tr>';
echo '<td>'. $row['model'] . '</td>';
echo '<td>'. $row['problem'] . '</td>';
echo '<td>'. $row['work'] . '</td>';
echo '<td>'. $row['result'] . '</td>';
echo '<td>'. $row['keywords'] . '</td>';
echo '<td>'. $row['addedby'] . '</td>';
echo '<td>'. $row['date_time'] . '</td>';
echo '<td>'. $row['document'] . '</td>';
}
I allowed users to add documents and the file name is being recorded into documents after string operations. I want to display respective documents as hyperlinks. If I was using mysql_fetch array I would use
<td> view </td>
but I am not good at PDO and getting synthax error everytime.
here is my erroneous code:
echo '<td>'. view file.'</td>';
simply try echo '<td>view file</td>';
Your echo statement mixes inline html with echo. You should use inline html or echo a string, but not both at the same time
echo '<td>view file</td>';
or
<td>view file</td>
echo '<td>view file</td>';
<!doctype html>
<?php
?>
<html>
<head>
<title>Midterm Review</title>
</head>
<body>
<h3>Tools Not Currently in Stock</h3>
<?php
$conn = mysqli_connect(This part works );
mysqli_select_db(this part works);
$query = "SELECT * FROM `midterm` WHERE stock='0'";
$result = mysqli_query($conn, $query);
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
foreach ($result as $row) {
$row = mysqli_fetch_array($result);
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
echo '</table>';
};
mysqli_close($conn);
?>
<!--<form method="post" action="midterm_confirmation.php">
<label>Part Number: </label><input type="text" name="partNumber" /><br />
<label>Description: </label><input type="text" name="description" /></br />
<label>Stock: </label><input type="number" name="stock" /><br />
<label>Price: </label><input type="text" name ="price" /></br />
<label>Received Date: </label><input type="text" name="receivedDate" /></br />
<input type="Submit" value="Add to Stock">
</form> -->
</body>
</html>
Basically my end result is that I am getting one table row, instead the two I am getting. Any suggestions? The table I have populates everything but is only running once, instead of posting for each line I have where stock is equal to zero.
See the snippet below. P.S. Don't mix templates and database layer, it's a bad smell...
<?php
// establish connection to $conn variable
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>';
mysqli_close($conn);
?>
The problem is in closing of </table> It should be kept outside the loop.
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
// echo '</table>';// this is wrong.
};
echo '</table>';// this is correct. closing table inside loop is wrong, do it outside the loop.
You can simply use a while loop to do it like below:
<?php
// establish connection to $conn
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>'; // this should be outside the loop
mysqli_close($conn);
?>
Or if you want to use a foreach then write an extra function for it:
<?php
function mysql_fetch_all($result) {
$rows = array();
while ($row = mysql_fetch_array($result)) {
$rows[] = $row;
}
return $rows;
}
// establish connection to $conn
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
foreach (mysql_fetch_all($result) as $row){
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>'; // this should be outside the loop
mysqli_close($conn);
?>
I have a render of row of database
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['tex1'] . '</td>';
echo '<td>' . $row['tex2'] . '</td>';
echo '<td>' . $row['tex3'] . '</td>';
echo '<td>' . $row['tex4'] . '</td>';
echo '<td>' . $row['tex5'] . '</td>';
echo '<td>' . $row['tex6'] . '</td>';
echo '<td>' . $row['tex7'] . '</td>';
echo '<td>' . $row['tex8'] . '</td>';
echo '<td>' . $row['tex9'] . '</td>';
echo '<td>' . $row['tex10'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
And in the tex1 is a date, my problem is for a class for tr, when the tex2 is empty and the date of tex1 is older than 30 days, tr have class red, the date of tex1 is inserted manually,how can I do to change the class on the basis of this?
I'm not sure if I understood what you wanted properly but I'll give it a shot. You want to add a class ("red") to the , whenever the condition is met that either the date is older than 30 days old (tex1) or the First Name is empty (tex2).
echo "<table border='1' cellpadding='10'>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
// I assume since you're just printing it out, the date is a String
// So first convert it to a timestamp
$date = strtotime($row['tex1']);
// Determine the difference, I'm doing it this way to make it easy for you to understand
$days = 60*60*24*30; // 30 days
if (empty($row['tex2']) || time()-$date > $days) {
echo '<tr>';
} else {
echo '<tr class="red">';
}
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['tex1'] . '</td>';
echo '<td>' . $row['tex2'] . '</td>';
echo '<td>' . $row['tex3'] . '</td>';
echo '<td>' . $row['tex4'] . '</td>';
echo '<td>' . $row['tex5'] . '</td>';
echo '<td>' . $row['tex6'] . '</td>';
echo '<td>' . $row['tex7'] . '</td>';
echo '<td>' . $row['tex8'] . '</td>';
echo '<td>' . $row['tex9'] . '</td>';
echo '<td>' . $row['tex10'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
You should check for the conditions what you have mentioned in your question. And this can be done like this.
if(strtotime($row['tex1']) + 30 * 24 * 60 * 60 < time() && $row['tex2'] =="") {
$class="class='red'";
} else {
$class= "";
}
echo "<tr $class>";// id condition satisfies, class='red' will get echo, else echo will be null.
Try a conditional when writing the element:
echo '<tr'.
( ((strtotime($row['tex1']) < strtotime('30 days ago'))
&& (empty($row['tex2']))) ? ' class="red"' : '').
'>';