Multiple SELECT variables are not displaying - php

Having issues displaying all of the selected data entries inside SELECT. Right now the only one that is displaying is the recipe_direct data. Each table data should list in one row the name, ingred, direct, auth name and auth email.
<div class="starter-template">
<h1>Recipes</h1>
</div>
<?php
try
{
$sql = 'SELECT recipe_id, recipe_name, recipe_ingred, recipe_direct, author_name, author_email FROM recipes';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching recipes: ' . $e->getMessage();
}
while ($row = $result->fetch())
{
$recipes[$row['recipe_id']] = $row;
}
?>
<p>Add a Recipe</p>
<?php foreach ($recipes as $id => $recipe): ?>
<blockquote>
<table class="table table-striped">
<tr>
<td><?php echo $recipe['recipe_name']; ?></td>
<td><?php echo $recipe['recipe_ingred']; ?></td>
<td><?php echo $recipe['recipe_direct']; ?></td>
<td><?php echo $recipe['author_name']; ?></td>
<td><?php echo $recipe['author_email']; ?></td>
</tr>
<?php htmlout($recipe_html); ?> |
edit |
delete
</table>
</blockquote>
<?php endforeach; ?>

You overwrite $recipe_html each time you assign a value to it
why not just echo out the table rows:
<table>
<?php foreach ($recipes as $id => $recipe): ?>
<tr>
<td><?= $recipe['recipe_name']; ?></td>
<td><?= $recipe['recipe_ingred']; ?></td>
<td><?= $recipe['recipe_direct']; ?></td>
<td><?= $recipe['author_name']; ?></td>
<td><?= $recipe['author_email']; ?></td>
</tr>
<?php endforeach; ?>
</table>
EdIt as #Fred -ii- states, the table tags should be outside the loop

Everything looks correct up until your foreach.
<blockquote>
<table class="table table-striped">
<?php foreach ($recipes as $id => $recipe): ?>
<tr>
<td><?php $recipe_html = $recipe['recipe_name']; ?></td>
<td><?php $recipe_html = $recipe['recipe_ingred']; ?></td>
<td><?php $recipe_html = $recipe['recipe_direct']; ?></td>
<td><?php $recipe['author_name']; ?></td>
<td><?php $recipe['author_email']; ?></td>
</tr>
<p> //seems you were missing opening <p> tag//
<?php htmlout($recipe_html); ?> |
edit | //can use shorthand here//
delete //can use shorthand here//
</p>
<?php endforeach; ?>
</table>
</blockquote>

Related

Is windows.setTimeout function getting executed in following code?

I want 3 elements of array to be displayed before setTimeout(30000) function and 3 elements after it;so that there is gap of 30 seconds between both the loops.But I am not getting the desired result.Following is the code:
<?php
$info=array(array("name"=>"abc",
"phone"=>"12345"),
array("name"=>"pqr",
"phone"=>"23456"),
array("name"=>"def",
"phone"=>"34567"),
array("name"=>"asd",
"phone"=>"45678"),
array("name"=>"ghj",
"phone"=>"56789"),
array("name"=>"jkl",
"phone"=>"67566"),
);
echo ( $info[0]["name"]);
?>
<body>
<table>
<tr>
<th>id</th>
<th>Name</th>
<th>phone<th>
</tr>
<?php
$i=1;
foreach ($info as $data) {
if($i==4)
{
break;
}
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $data["name"]; ?></td>
<td><?php echo $data["phone"]; ?></td>
</tr>
<?php $i++;}?>
<script>
window.setTimeout(30000);
</script>
<?php
for($j=3;$j<6;$j++)
{
?>
<tr>
<td><?php echo $j+1; ?></td>
<td><?php echo $info[$j]["name"]; ?></td>
<td><?php echo $info[$j]["phone"]; ?></td>
</tr>
<?php } ?>
</table>

php table not displaying contents and only creating table boxes

I would like the table to display all the values inside of it, but it is currently not displaying the values and only creating an empty table
[what the table is displayed as (image)][1]
$result = mysqli_query( $conn,'SELECT * FROM Pictures ');
$conn->close();
Html
<html>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;"
<thead>
<tr>
<th>id</th>
<th>Name</th>
<th>image</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
if( $result != null){
Result is not empty
while( $row1 = mysqli_fetch_assoc( $result ) ){
foreach ($row1 as $row){
?>
<tr>
<td><?php $row['id'] ?></td>
<td><?php $row['hname'] ?></td>
<td><?php $row['himage'] ?></td>
<td><?php $row['hdesc'] ?></td>
</tr>
<?php
}
}
}else{
echo "Something went wrong with the result";
}
?>
</tbody>
</table>
<?php //mysqli_close($conn); ?>
</body>
</html>
changed the out put to match the answer you gave but the output came out as picture 2 while my table is actually picture 3 any ideas
output:
table im trying to display
Display the data in this way:
<td><?php echo $row['id']; ?></td>
Try changing
<td><?php $row['id'] ?></td>
<td><?php $row['hname'] ?></td>
<td><?php $row['himage'] ?></td>
<td><?php $row['hdesc'] ?></td>
to
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['hname']; ?></td>
<td><?php echo $row['himage']; ?></td>
<td><?php echo $row['hdesc']; ?></td>
or the shorthand variant
<td><?= $row['id']; ?></td>
<td><?= $row['hname']; ?></td>
<td><?= $row['himage']; ?></td>
<td><?= $row['hdesc']; ?></td>
And as Samuel pointed out in a comment, are you sure there is a need for the extra foreach considering you're already looping with the while?
Update: OP have you tried the following?
while( $row = mysqli_fetch_assoc( $result ) ){
//removed foreach()
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['hname']; ?></td>
<td><?php echo $row['himage']; ?></td>
<td><?php echo $row['hdesc']; ?></td>
</tr>
<?php
}
}else{
Update 2 OP wishes to have the image load instead of showing the raw URL.
To do this, we need to use an actual image tag and insert the url into the src.
I assume that this line is the image URL
<td><?php echo $row['himage']; ?></td>
So change it to
<td> <img src="<?php echo $row['himage']; ?>" > </td>

Change row color of a table based on mysqli value using materializecss

I'm using Materialize css on my project, i have a table that shows mysql field called "status" and in this table i want change the color of the row if i change the "status" like "1=blue, 2=red..." Someone here knows how i can make a function to do this? Thank you.
table extample:
table class="striped bordered responsive-table">
<thead>
<tr>
<th>ID</th>
<th>Cliente</th>
<th>Objeto</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while($row_os = mysqli_fetch_assoc($result_user)){?>
<tr>
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
</tbody>
One simple way would be:
<?php
$colorMap = [
1 => 'blue',
2 => 'red',
// add more
];
while ($row_os = mysqli_fetch_assoc($result_user)) { ?>
<tr style="background:<?php echo $colorMap[$row_os['status']] ?>">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
Of course you could also add a class depending on status the same way and do the styling in CSS.
Simple as Doing this on the row , only if the status must contain a value of either 1 or 2
<?php
while($row_os = mysqli_fetch_assoc($result_user)){?>
<tr class="<?php echo $row_os["status"]==1?'blue':'red'?> lighten-2">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
or if status varies (then follow #colburton answers ) or this
<?php
while($row_os = mysqli_fetch_assoc($result_user)){
$color="";
switch($row_os["status"]){
case 1:
$color="blue";
break;
case 2:
$color="red";
break;
//and so on
}
?>
<tr class="<?php echo $color;?> lighten-2">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>

Diffrent query in one function codeigniter

I use Codeigneter and I make a search function and in my function I want run two diffrent query and I want show them in diffrent table, How I can do that Sorry I can't show the data
This is My code:
My controller
function showDetail(){
// Retrieve the posted search term.
$detailTiket = $this->input->get('ticket_id');
// Use a model to retrieve the results.
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data1["result"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data,$data1);
}
My Model
function showDetail($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$showDetailTiket=$this->db->query("My Query1");
return $detailTiketDown->result();
}
function showDetail2($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$detailTiketDown=$this->db->query("My query2");
return $detailTiketDown->result();
}
My view
<table Width='800'>
<?php
foreach($data as $row){?>
<tbody>
<tr>
<td><?php echo $row->ticket_id; ?></td>
</tr>
<tr>
<td><?php echo $row->created_time; ?></td>
</tr>
<tr>
<td><?php echo $row->start_IT; ?></td>
</tr>
<tr>
<td><?php echo $row->estimasi_selesai; ?></td>
</tr>
<tr>
<td><?php echo $row->name; ?></td>
</tr>
<tr>
<td><?php echo $row->description; ?></td>
</tr>
<tr style="background-color: cyan">
<td><b><?php echo $row->Status; ?></b></td>
</tr>
</tbody>
<?php } ?>
</table>
</center>
</div>
<div>
<table Width='1000'>
<?php foreach($data1 as $rows){ ?>
<tbody>
<tr>
<td><?php echo $rows->Tgl_Waktu; ?></td>
<td><?php echo $rows->PIC; ?></td>
<td><?php echo $rows->Tracking_Ticket; ?></td>
<td><?php echo $rows->Keterangan_Ticket; ?></td>
<td><?php echo $rows->File_Pendukung; ?></td>
</tr>
</tbody>
<?php }?>
</table>
You can pass data like Associative array to view
change your controller like this
Controller:
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data["result1"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data);
view:
Table1 :
foreach($result as $row)
{
//for first result
}
Table2:
foreach($result1 as $row1)
{
//for second result
}

Can't put data from database to a table with PHP

My problem is that I can't fill my table with the data I got in my database. I wrote a code in PHP to do it but it doesn't work and I don't know how to make it work. Any idea ? Here's my code :
view gestJury :
<h3>Liste des jurys :</h3>
<div class="tablebox">
<table>
<thead>
<tr>
<th>N°</th>
<th>Professeur1</th>
<th>Professeur2</th>
<th>Salle</th>
<th>Heure</th>
<?php if($rang == 0){ ?>
<th class="action" >Supprimer</th>
<?php } ?>
</tr>
</thead>
<?php $i = 0; ?>
<?php while($donnees = $listeJury->fetch(PDO::FETCH_ASSOC)){
$i = $i + 1;
?>
<tbody>
<tr class="row0">
<td><?php echo $donnees['idJury'] ?></td>
<td><?php echo $donnees['idProf'] ?></td>
<td><?php echo $donnees['idProf2'] ?></td>
<td><?php echo $donnees['Salle'] ?></td>
<td><?php echo $donnees['horaireJury'] ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
index.php :
function listJuryGlobal(){
$jury = new Professeur();
$listeJury = $jury->getJuryGlobal();
require('view/gestJury.php');
}

Categories