PHP query not returning all results - php

I'm trying to display a table of dates, albums and reviews made by users. The date and review show up with no problem, but at the moment my query is only displaying the first CD Title in the list.
Table 'cdreview' has columns 'CDID', 'userID', 'reviewDate', 'reviewText'.
Table 'cd' has 'CDTitle', so I've used a natural JOIN to link the 2 tables by the CDID, but I can't display the correct CDTitle.
Any help would be extremely grateful.
<?php
require_once 'database_conn.php';
$userid = $_SESSION['userSession'];
$sql = "SELECT * FROM cdreview JOIN cd WHERE '$userid'=cdreview.userID ORDER BY reviewDate ASC";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$row = mysqli_fetch_assoc($result);
$date = $row['reviewDate'];
$album = $row['CDTitle'];
$review = $row['reviewText'];
$cdid = $row['CDID'];
?>
<tr align='center'>
<td><?php echo $date;?></td>
<td><?php echo $album;?></td>
<td><?php echo $review;?></td>
<td><a href="edit_review.php?id=<?php echo $cdid;?>">Edit</td>
<td><a href="album.php?id=<?php echo $cdid;?>">Delete</td>
</tr>
</table>

You have to iterate through results :
while ($row = mysqli_fetch_assoc($result) ){
$date = $row['reviewDate'];
$album = $row['CDTitle'];
$review = $row['reviewText'];
$cdid = $row['CDID'];
// print stuff
}

mysqli_fetch_assoc is returning a result set.
You have to loop through this result set and handle each result separately.
while ($row = mysqli_fetch_assoc($result)) {
$date = $row['reviewDate'];
$album = $row['CDTitle'];
$review = $row['reviewText'];
$cdid = $row['CDID'];
?>
<tr align='center'>
<td><?php echo $date; ?></td>
<td><?php echo $album; ?></td>
<td><?php echo $review; ?></td>
<td><a href="edit_review.php?id=<?php echo $cdid; ?>">Edit</td>
<td><a href="album.php?id=<?php echo $cdid; ?>">Delete</td>
</tr>
<?php
}

Related

echo first row of data into page title

I have a table of contacts and a search form the returns the contact searched for. All work fine as it should. But is it possible to show an instance of the first row only in a separate div or span at top of page? I have seen this done in MS Access, is it possible in php? Here is my search query but I have no idea how to echo or show the first row only into a title or heading div.
$sql = "SELECT * FROM people";
if( isset($_GET['search']) ){
$name = mysqli_real_escape_string($con, htmlspecialchars($_GET['search']));
$name2 = mysqli_real_escape_string($con, htmlspecialchars($_GET['search2']));
$sql = "SELECT * FROM people WHERE first_name ='$name' AND last_name= '$name2'";
}
$result = $con->query($sql);
?>
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['people_id']; ?></td>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['contact']; ?></td>
</tr>
}
I just would do it like this (updated answer):
$tableData = [];
$divData = "";
while ($row = $result->fetch_assoc()) {
// empty tableData --> first row
if (empty($tableData)) {
// do your special for the first row here (div)
$divData = "hello world! ".$row['first_name']." ".$row['last_name'];
}
$tableData[] ="<tr>
<td>".$row['people_id']."</td>
<td>".$row['first_name']."</td>
<td>".$row['last_name']."</td>
<td>".$row['address']."</td>
<td>".$row['contact']."</td>
</tr>";
}
echo "<div>".$divData."</div>";
echo "<table>";
echo implode("\n", $tableData);
echo "</table>";

Rearrange table head data to match with table body

I want to arrange the table head with my table body, so the data fill have a same type, how can i doing it??
The black square is the correct one
This is my code
<table>
$syntax = 'SELECT customer,type, count(type) as num FROM view_detail GROUP BY type ORDER BY customer';
$rows = $config->prepare($syntax);
$rows->execute();
$result = $rows->fetchAll(PDO::FETCH_OBJ);
?>
</tr>
<td>No</td>
<td>Customer</td>
<?php for($x=0;$x<count($result);$x++){ ?>
<td><?php echo $result[$x]->type; ?></td>
<?php } ?>
<td>Total</td>
<?php $no = 1;
$query = 'SELECT customer,type, count(type) as tipe FROM view_detail GROUP BY customer ORDER BY customer';
$baris = $config->prepare($query);
$baris->execute();
$hasil1 = $baris->fetchAll(PDO::FETCH_OBJ);
for($i=0;$i<count($hasil1);$i++){
$sql = 'SELECT * FROM view_detail WHERE customer = "'.$hasil1[$i]->customer.'" ORDER BY customer';
$row = $config->prepare($sql);
$row->execute();
$hasil = $row->fetchAll(PDO::FETCH_OBJ);
?>
<tr>
<td><?php echo $no; $no++;?></td>
<td><?php echo $hasil[$i]->customer; ?></td>
<?php
for($y=0;$y<count($result);$y++){
if($y < count($hasil)){
?>
<td><?php echo $hasil[$y]->type; ?></td>
<?php }else{ ?>
<td>0</td>
<?php } } }?>
</tr>
</table>
Thanks
I want to make it like this
Edit:
Dynatrade -> MFN200, N50Z, and N70
Super Bloom -> N70, MFN100, MFN120, and MFN150

duplicate on my PDO query

Here's my code sir:
<?php
session_start();
include 'include/db_config.php';
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
?>
<tr class="record">
<td><?php echo $row2['firstname'];?></td>
<td><?php echo $row['no_guest']; ?></td>
<td><?php echo $row['type_service']; ?></td>
<td><?php echo $row['datepicker']; ?></td>
<td><?php echo $row['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
}
?>
i didnt use a LEFT JOIN for displaying data's from 2 tables. i just want to do it in my own way . But my problem is it duplicates my data . before i inserted my second query its just 2 data's and now its 4 already. i just cant figure it out where is the duplication occurs.
Someone help me out please . Thanks in Advance.
You display data in second for, which is inside first for. So you get result count of customer_info table length*service_info length results. You should save info in arrays and then use only one for cycle. Eg.:
<?php
session_start();
include 'include/db_config.php';
$services = array();
$customers = array();
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$services[] = $row;
}
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
$customers[] = $row2;
}
foreach($services as $key => $service) {
?>
<tr class="record">
<td><?php echo $customers[$key]['firstname'];?></td>
<td><?php echo $service['no_guest']; ?></td>
<td><?php echo $service['type_service']; ?></td>
<td><?php echo $service['datepicker']; ?></td>
<td><?php echo $service['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
?>
Btw, i suggest you use different approach, like joins

getting value of array check box and textbox in table

i'm a beginner in php and i'm hoping that somebody here can help me in this.
i have a table that has a check box to select an item and a text box to indicate the quantity the person wants to borrow. i was wondering how i can retrieve both these data and then save them in my database.
here is a part of my code:
<td width="30">
<input id="optionsCheckbox" class="uniform_on" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
</td>
<td><?php echo $row['item_code']; ?></td>
<td><?php echo $row['item_name']; ?></td>
<td align="center">
<img class="img-rounded" src="<?php echo $row['item_image'];?>" border="0" onMouseOver="showtrail('<?php echo $row['item_image'];?>','<?php echo $row['item_code'].": ".$row['item_name'];?> ',200,5)" onMouseOut="hidetrail()"></a></td>
<td><?php echo $row['item_quantity'] - $row['item_consumption']; ?> <?php echo $row['unit']; ?></td>
<td><input type="text" name="consume[]" pattern="[0-9]{1,4}"/></td>
here's what i have so far:
$id=$_POST['selector'];
$consume= $_POST['consume'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
$query = $conn->query("select * from item where item_id ='$id[$i]'")or die(mysql_error());
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
echo $code; echo $name;
echo $id;
}
$x = count($consume);
for($y=0; $y < $x; $y++)
{echo $consume;
}
the echos are just for checking if the data goes through. i'm just making sure they do before i make a query for table insertion.
what i'm trying to do is to post them on another page, display them in a table (a la shopping cart) and then make the borrower fill out a form with his details.
ok, using gul's answer below, this is what i did:
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$ids = ''; $consumes='';
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$ids .= $id[$i].',';
$consumes.="'".$consume[$i]."',";
}
//remove the last comma
$ids = substr($ids,0,-1);
$consumes = substr($consumes,0,-1);
$query = $conn->query("select * from item where item_id IN($ids)")or die(mysql_error());
//table element
echo "<table border='1' style='border-collapse:
collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='150' align='center'>Item Code</td>";
echo "<td width='150' align='center'>Item Name</td>";
echo "<td width='150' align='center'>Quantity</td>";
echo "</tr>";
//get data in table
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
$table ="";
$table.="<tr>";
$table.="<td>".$code."</td>";
$table.="<td>".$name."</td>";
$table.="<td>".$consumes."</td>";
$table.="</tr>";
$table.="</table>";
//echo the table
echo $table;
however, when i select multiple items, only the last one shows up, but the consumes show up like this:
consume shows in one cell with single quotes and comma. help?
Try like this:
//first get the post
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$ids = ''; $consumes='';
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$ids .= $id[$i].',';
//$consumes.="'".$consume[$i]."',";
}
//remove the last comma
$ids = substr($ids,0,-1);
$query = $conn->query("select * from item where item_id IN($ids))or die(mysql_error());
//table element
$table = "<table border='1'>";
$table.="<tr>";
$table.="<th>Code</th><th>Name</th>";
$table.="</tr>";
//get data in table
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
$table.="<tr>";
$table.="<td>".$code."</td>";
$table.="<td>".$name."</td>";
$table.="</tr>";
$table.="</table>";
//echo the table
echo $table;
ok, so i tried this and it worked. thanks to gul for helping me.
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$data = array();
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$row = array('selector'=>$id[$i],'consume'=>$consume[$i]);
//push in array
array_push($data,$row);
$query = $conn->query("select * from item where item_id ='$id[$i]'")or die(mysql_error());
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
while ($row = $query->fetch()) {
$id = $row['id'];
}
?>
<tr>
<td><?php echo $code; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $consume[$i]; ?></td>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>

Issue with show all users in the table with pdo

I have the next issue, when I need the code show me all the users in the table always show me one data the first one or the last one if I change the ASC to DESC inside of SELECT..
I need to show me all users... can you please help me with this?
Here the code and the table with the row I need to show:
<?
include '../include/config.php';
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
$id_paciente = $row['id_paciente'];
$id_tipo = $row['id_tipo'];
$nombre = $row['nombre'];
$apellido = $row['apellido'];
$ciudad = $row['ciudad'];
$telefono = $row['telefono'];
$foto = $row['foto'];
}
?>
<tr>
<th><?php echo $id_paciente; ?></th>
<td><img src="../<?php echo $foto;?>" class="image_thumbnail" /></td>
<td><?php echo $nombre; ?></td>
<td><?php echo $apellido; ?></td>
<td><?php echo $id_tipo; ?></td>
<td><?php echo $ciudad; ?></td>
<td><?php echo $telefono; ?></td>
You are echoing your variables outside of the loop.
So, move it inside:
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
?>
<tr>
<th><?php echo $row['id_paciente'] ?></th>
<td><img src="../<?php echo $row['foto']?>" class="image_thumbnail" /></td>
<td><?php echo $row['nombre'] ?></td>
<td><?php echo $row['apellido'] ?></td>
<td><?php echo $row['id_tipo'] ?></td>
<td><?php echo $row['ciudad'] ?></td>
<td><?php echo $row['telefono'] ?></td>
<tr>
<? } ?>
well I get my answer with my problem...
now I see all the users, the code neccesary is:
<?
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
?>
for somebody want to
Best Regards!

Categories