What´s is wrong on this sentence?
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['u_id'] . '</td>';
echo '<td>'. $row['u_role'] . '</td>';
echo '<td>'. $row['u_name'] . '</td>';
echo '<td>'. $row['u_passw'] . '</td>';
echo '<td>'. $row['u_init'] . '</td>';
echo '<td>'. $row['c_name'] . '</td>';
echo '<td>'. $row['u_mail'] . '</td>';
echo '<td>'.'<img href="'. $row['u_pic'] . '" width=45 height=45></img>'.'</td>';`
}
I´m able to print all fields after query but for last one is not possible, I got on db saved the url for the picture with this format
http://localhost/Pics/st.gif dbfield as varchar
If I inspect the component in the browser I can see it was properly selected by mysql but there is not print on the screen.
Any comment? Thanks in advance
For images you'll have to use:
<img src="url">
echo '<td><img src="'. $row['u_pic'] . '" width=45 height=45></img></td>';
It should be in following way,
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['u_id'] . '</td>';
echo '<td>'. $row['u_role'] . '</td>';
echo '<td>'. $row['u_name'] . '</td>';
echo '<td>'. $row['u_passw'] . '</td>';
echo '<td>'. $row['u_init'] . '</td>';
echo '<td>'. $row['c_name'] . '</td>';
echo '<td>'. $row['u_mail'] . '</td>';
echo '<td>'.'<img src="'. $row['u_pic'] . '" width=45 height=45></img>'.'</td>';
}
Related
i have one table in locallhost ,In my table there is an item id,Now I want to send the id Related row to another page by clicking on EDIT . Thankful
The part of the program I think is difficult to write below
....
....
$response["travel"]=array();
while ($row = mysql_fetch_array($sql)) {
// Print out the contents of the entry
echo '<tr>';
echo '<td class="text-center">' . $i . '</td>';
echo '<td class="text-center">' . $row['companyname'] . '</td>';
echo '<td class="text-center">' . $row['cod'] . '</td>';
echo '<td class="text-center">' . $row['bigan'] . '</td>';
echo '<td class="text-center">' . $row['stop'] . '</td>';
echo '<td class="text-center">' . $row['date'] . '</td>';
echo '<td class="text-center">' . $row['time'] . '</td>';
echo '<td class="text-center">' . $row['price'] . '</td>';
echo '<td class="text-center">' .'EDIT' .'</td>';
$i++;
.......
.....
You don't use <?php inside strings, you use that when you've gone back into HTML mode. You should just concatenate the variable, like you do everywhere else
echo '<td class="text-center">' .'ویرایش' .'</td>';
I am at the internship and I am developing a HTML table that has milestones and milestoneparts(in this project we call it milestonefase). Each milestone has a bunch of milestonefases.
As example: Milestone99 has milestonefase 10, milestonefase14.
Those data come from two diffrent database tables.
My array looks like this:
Array
(
[milestonesfases] => Array
(
[10] => Array
(
[milestonefase_id] => int
[milestonefase_titel] => string
[milestonefase_milestone_id] => int
)
[14] => Array
(
[milestonefase_id] => int
[milestonefase_titel] => string
[milestonefase_milestone_id] => int
)
)
[milestone_id] => int
[milestone_titel] => string
[client] => string
[milestone_verkocht_id] => int
)
My question is: How can I loop trough all milestonefases that is connected with milestone 6 (in this case)
My loop looks like this:
foreach ($stones as $milestone)
{
echo '<tr id="'. $milestone['milestone_id'] . '" class="milestone'. $milestone['milestone_id'] . '">';
echo '<td>'. $milestone['milestone_id'] . '</td>';
echo '<td>' . $milestone['milestone_titel'] . '</td>';
echo '<td>'. $milestone['client'] . '</td>';
echo '</tr>';
for ($i = 0; $i < count($milestone['milestonesfases']); $i++)
{
echo '<tr>';
echo '<td>'. $milestone['milestonesfases']['milestonefase_id'] . '</td>';
echo '<td>'. $milestone['milestonesfases'][10]['milestonefase_titel'] . '</td>';
echo '<td></td>';
echo '</tr>';
}
}
That '10' in my loop needs to be all milestonefase_id's (which is 10, 11, 12, 13, 14, 15 in this milestone).
How can I manage to get all milestonefases.
Attention! it must work for every milestones not only this
please
That piece of milestones works but milestonefases does not work.
This is Check for if is array and you can use it using same foreach like #Aron Said:
foreach ($stones as $milestone)
{
echo '<tr id="'. $milestone['milestone_id'] . '" class="milestone'. $milestone['milestone_id'] . '">';
echo '<td>'. $milestone['milestone_id'] . '</td>';
echo '<td>' . $milestone['milestone_titel'] . '</td>';
echo '<td>'. $milestone['client'] . '</td>';
echo '</tr>';
if(is_array($milestone['milestonesfases'])){
foreach ($milestone['milestonesfases'] as $key => $value) {
echo '<tr>';
echo '<td>'. $value['milestonefase_id'] . '</td>';
echo '<td>'. $value['milestonefase_titel'] . '</td>';
echo '<td></td>';
echo '</tr>';
}
}
}
Instead of using a for loop,
for ($i = 0; $i < count($milestone['milestonesfases']); $i++)
Do a foreach loop
foreach ($milestone['milestonesfases'] as $milestonesfase)
{
echo '<tr>';
echo '<td>'. $milestonesfase['milestonefase_id'] . '</td>';
echo '<td>'. $milestonesfase['milestonefase_titel'] . '</td>';
echo '<td></td>';
echo '</tr>';
}
please try
foreach ($stones as $milestone)
{
echo '<tr id="'. $milestone['milestone_id'] . '" class="milestone'. $milestone['milestone_id'] . '">';
echo '<td>'. $milestone['milestone_id'] . '</td>';
echo '<td>' . $milestone['milestone_titel'] . '</td>';
echo '<td>'. $milestone['client'] . '</td>';
echo '</tr>';
foreach($milestone['milestonesfases'] as $milestoneRow)
{
if($milestoneRow['milestonefase_milestone_id'] == $milestone['milestone_id']) {
echo '<tr>';
echo '<td>'. $milestoneRow['milestonefase_id'] . '</td>';
echo '<td>'. $milestoneRow['milestonefase_titel'] . '</td>';
echo '<td></td>';
echo '</tr>';
}
}
}
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);
?>
how can i do if this row is empty then it will showing another row
suppose name row is blank
echo '<td>' . $row['name'] . '</td>';
so if name row is blank then it will show echo '<td>' . $row['companyname'] . '</td>'; row
please help me how can i fix this
thanks
echo "<tr>";
echo '<td>' . $row['buildingname'] . '</td>';
echo '<td>' . $row['area'] . '</td>';
echo '<td>' . $row['city'] . '</td>';
echo '<td>' . $row['flatno'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['mobileno'] . '</td>';
echo '<td>' . $row['agreementdates'] . '</td>';
echo '<td>' . $row['agreementdatee'] . '</td>';
echo "</tr>";
Try
if(empty( $row['name'])){
$tbl = '<td>' . $row['companyname'] . '</td>';
}else{
$tbl = '<td>' . $row['name'] . '</td>';
}
echo $tbl;
try this i think this is working.
echo '<td>' . empty($row['name']) ? $row['companyname'] : $row['name'] . '</td>';
try this:
echo "<tr>";
if(!empty($row['buildingname'])) {
echo '<td>' . $row['buildingname'] . '</td>';
}
if(!empty($row['area'])) {
echo '<td>' . $row['area'] . '</td>';
}
if(!empty($row['city'])) {
echo '<td>' . $row['city'] . '</td>';
}
if(!empty($row['flatno'])) {
echo '<td>' . $row['flatno'] . '</td>';
}
if(!empty($row['name'])) {
echo '<td>' . $row['name'] . '</td>';
}
if(!empty($row['mobileno'])) {
echo '<td>' . $row['mobileno'] . '</td>';
}
if(!empty($row['agreementdates'])) {
echo '<td>' . $row['agreementdates'] . '</td>';
}
echo "</tr>";