Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
When i run it. i only see 1 records of customer and it says that the error is in " WHILE($cus = mysql_fetch_array($cus)){" Line.. anyone knows how to solve it? ..tnx
<table id="datatables" class="display">
<thead>
<tr>
<th>ID</th>
<th>FullName</th>
<th>Age</th>
<th>Gender</th>
<th>Email</th>
<th>Barangay</th>
<th>CompleteAddress</th>
<th>Username</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<?php $cus = mysql_query("SELECT * FROM customer") or die(mysql_error()); ?>
<?php
WHILE ($cus = mysql_fetch_array($cus)) {
$id = $cus['cus_id'];
$email = $cus['email'];
$control = $cus['cus_id'];
$user = $cus['username'];
$pass = $cus['password'];
$brgy = $cus['barangay'];
$comadd = $cus['com_address'];
$age = $cus['age'];
$gend = $cus['gender'];
$fname = $cus['firstname'] . " " . $cus['middlename'] . " " . $cus['lastname'];
?>
<tr class="gradeA del<?php echo $id; ?>">
<td><?php echo $control; ?></td>
<td><?php echo $fname; ?></td>
<td><?php echo $age; ?></td>
<td><?php echo $gend; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $brgy; ?></td>
<td><?php echo $comadd; ?></td>
<td><?php echo $user; ?></td>
<td><?php echo $pass; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
You are missing the closing statement for your while loop. Add this to the bottom
<?php } ?>
Second issue
while($cus = mysql_fetch_array($cus)) { // You are overwriting the recordset
Change the variable name there
while($cusTemp = mysql_fetch_array($cus)) { // or any other name convenient
Please close your missing while loop curly braces.
Complete code:-
<?php
while($rows = mysql_fetch_array($cus)) {
$id = $rows['cus_id'];
$email = $rows['email'];
$control = $rows['cus_id'];
$user = $rows['username'];
$pass = $rows['password'];
$brgy = $rows['barangay'];
$comadd = $rows['com_address'];
$age = $rows['age'];
$gend = $rows['gender'];
$fname = $rows['firstname'] . " " . $rows['middlename']. " " . $rows['lastname'];
?>
<tr class="gradeA del<?php echo $id;?>">
<td><?php echo $control; ?></td>
<td><?php echo $fname; ?></td>
<td><?php echo $age; ?></td>
<td><?php echo $gend; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $brgy; ?></td>
<td><?php echo $comadd; ?></td>
<td><?php echo $user; ?></td>
<td><?php echo $pass; ?></td>
</tr>
<?php } ?>
you can't use $cus as the result set for the query and the row information. one of these variables needs to be something different. in my comment above, I suggested turning one into row but it's a lot easier to just change the name of the result set, so that is what I did in the code below.
I assume you have the correct connection set up before your pasted code starts, too.
<table id="datatables" class="display">
<thead>
<tr>
<th>ID</th>
<th>FullName</th>
<th>Age</th>
<th>Gender</th>
<th>Email</th>
<th>Barangay</th>
<th>CompleteAddress</th>
<th>Username</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<?php
// this used to be $cus but I renamed it...
$customer_results = mysql_query("SELECT * FROM customer") or die(mysql_error());
// here was the issue... you were using $cus twice.
WHILE ($cus = mysql_fetch_array($customer_results))
{
$id = $cus['cus_id'];
$email = $cus['email'];
$control = $cus['cus_id'];
$user = $cus['username'];
$pass = $cus['password'];
$brgy = $cus['barangay'];
$comadd = $cus['com_address'];
$age = $cus['age'];
$gend = $cus['gender'];
$fname = $cus['firstname'] . " " . $cus['middlename'] . " " . $cus['lastname'];
?>
<tr class="gradeA del<?php echo $id; ?>">
<td><?php echo $control; ?></td>
<td><?php echo $fname; ?></td>
<td><?php echo $age; ?></td>
<td><?php echo $gend; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $brgy; ?></td>
<td><?php echo $comadd; ?></td>
<td><?php echo $user; ?></td>
<td><?php echo $pass; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Keep in mind, mysql functions are old and will be removed from PHP. You should not waste your time learning them. Instead use PDO or MYSQLI. Personally, I think PDO is easier. If you decide to switch over to one of those two new DB classes, be sure to parameterize your queries and bind values to help prevent SQL injection.
You get from $cus, and assign it to $cus as well. This means you will see 1 record, and afterwards it fails on the next fetch since you changed the context:
while($cusdata = mysql_fetch_assoc($cus)){
Would be better :)
What about this code:
<?php while (($row = mysql_fetch_array($cus)) !== false) { ?>
<tr>
<tr class="gradeA del<?php echo $row['cus_id'];?>">
<td><?php echo $row['cus_id']; ?></td>
<td><?php echo $row['firstname'] . " " . $row['middlename']. " " . $row['lastname']; ?></td>
<td><?php echo $row['age']; ?></td>
<td><?php echo $row['gender']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['barangay']; ?></td>
<td><?php echo $row['com_address']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['password']; ?></td>
</tr>
<?php } ?>
The code is clean, condition is properly checked against FALSE and no need for useless lines of code...
Related
So this is my delete link
<?php
while ($res = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $res['ENo']; ?></td>
<td><?php echo $res['Lastname']; ?></td>
<td><?php echo $res['Firstname']; ?></td>
<td><?php echo $res['MI']; ?></td>
<td><?php echo $res['Address']; ?></td>
<td><?php echo $res['Gender']; ?></td>
<td><?php echo $res['MobileNo']; ?></td>
<td><?php echo $res['Position']; ?></td>
<td>
Delete
</td>
</tr>
<?php endwhile; ?>
and this is my SQL query
if (isset($_GET['delete']))
{
$id = $_GET['delete'];
$d = $mysqli_query($db,"DELETE FROM `tbllist` WHERE ENo = $id") or die($mysqli->error());
if($d){
header("location:index.php");
}
}
it seems ok in code but its not deleting the data in the database please help its my project for my programming subject in school
Change
$mysqli_query($db,"DELETE FROM `tbllist` WHERE ENo = $id")
to
$mysqli->query("DELETE FROM `tbllist` WHERE ENo = $id")
data i am fetching from infusion using tag id and dsfind() function. and that data i have to divide into 3 pages. (data used be unique)
<?php
require("isdk.php");
$app = new iSDK;
if ($app->cfgCon("infusionid"))
{
$returnFields = array('Id','Email');
$contacts = $app->dsFind('Contact',14,0,'Groups',7406,$returnFields);
echo '<pre>', print_r($contacts), '</pre>';
}
?>
I want to print this series
<tr>
<td><?php echo $contacts[0]['Id'] ?></td>
<td><?php echo $contacts[0]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[4]['Id'] ?></td>
<td><?php echo $contacts[4]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[7]['Id'] ?></td>
<td><?php echo $contacts[7]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[11]['Id'] ?></td>
<td><?php echo $contacts[11]['Email']; ?></td>
</tr>
You can use foreach() function e.g:
echo "<table>";
foreach ( $contacts as $var) {
echo "<tr><td>ID: ", $var['Id']."</td>";
echo "<td>Email: ", $var['Email']."</td></tr>";
}
echo "</table>";
I have a little issue when I try to to loop my php values in HTML. So far this is what I tried but I have not excpected result.
If I remove the loop I only get the first entry. I would like to echo all the possibles entries from my research.
This is my code ( from an SQL request).
<html>
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Number</th>
<th>Adress</th>
<th>link</th>
<!--<th class="glyphicon glyphicon-pencil"></th>-->
</tr>
<tr>
<?php $rowG = oci_fetch_array($stid, OCI_RETURN_NULLS);?>
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
</tr>
</table>
</html>
Do you know where I made my mistake ?
Thank you for your help
Edit : Finally I managed to do it by using a do{}while loop.
Thank you all for your help
RFlow
It's hard to guess what you are trying to do or even what actually happens, since I don't know what is assigned to $rowG, so I tried to hack meaning out of this from the code's errors and came up with that :
<?php
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]; ?></td>
<td><?php echo ' Link '; ?></td>
</tr>
<?php
}
?>
If it doesn't work with you, you'll have to provide the informations that should have been included in your question since the begining.
This is basic iteration over query results:
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
</tr>
<?php
}
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Would be :
<?php foreach($rowG as $arr): ?> <tr>
<td><?php echo $arr[2]; ?></td>
<td><?php echo $arr[1]; ?></td>
<td><?php echo $arr[0]; ?></td>
<td><?php echo $arr[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$arr[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Note the use of $arr instead of $rowG.
In the original code $array is not used.
I'm trying to display multiple arrays into a table so that each value is on a separate line within the table.
This is my current setup:
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");
while($res = mysqli_fetch_array($result))
{
?>
<tr>
<td><? echo $res['partnumber']; ?></td>
<td><? echo $res['partdescription']; ?></td>
<td><? echo $res['partprice']; ?></td>
<td><? echo $res['partquantity']; ?></td>
<?php
}
?>
Which displays the following:
Screenshot
instead i need it to display each value on a separate line
I tried the following but it duplicates its value over and over again.
<? foreach ($res as $row) : ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
<td><? echo $row['partdescription']; ?></td>
<td><? echo $row['partprice']; ?></td>
<td><? echo $row['partquantity']; ?></td>
</tr>
<? endforeach;
}
?>
First of all, your code is vulnerable to SQL injection, make sure to secure $id (ex. $id = (int)$_GET['id'];) before putting it into your query string.
then try:
while($res = mysqli_fetch_array($result))
{
?>
<tr>
<td><? echo $res['partnumber']; ?></td>
</tr><tr>
<td><? echo $res['partdescription']; ?></td>
</tr><tr>
<td><? echo $res['partprice']; ?></td>
</tr><tr>
<td><? echo $res['partquantity']; ?></td>
</tr>
<?php
}
?>
If you need new line for each column must simply add new tr for each td
Try this:
<? foreach ($res as $row) : ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
</tr>
<tr>
<td><? echo $row['partdescription']; ?></td>
</tr>
<tr>
<td><? echo $row['partprice']; ?></td>
</tr>
<tr>
<td><? echo $row['partquantity']; ?></td>
</tr>
<? endforeach; ?>
Notice the change in the starting curly braces, and that i removed the endforeach you used, and the table tags...
Now you can make use of this in your for each loop.
<table>
<? foreach ($res as $row) { ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
<td><? echo $row['partdescription']; ?></td>
<td><? echo $row['partprice']; ?></td>
<td><? echo $row['partquantity']; ?></td>
</tr>
<?
}
?>
</table>
I think this should do the trick
This is what i'm getting with your code:
(i'll fix the security issue after i get this working, thank you)
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");
while($res = mysqli_fetch_array($result))
{
?>
<? foreach ($res as $row) { ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
<td><? echo $row['partdescription']; ?></td>
<td><? echo $row['partprice']; ?></td>
<td><? echo $row['partquantity']; ?></td>
</tr>
<?
}
}
?>
$arr1 = ['name'=>'jon', 'age'=>30, 'address'=>'dilly'];
$arr2 = ['name'=>'gita', 'age'=>20, 'address'=>'goha'];
$arr3 = ['name'=>'sita', 'age'=>20, 'address'=>'pune'];
$agroup = array([$arr1, $arr2, $arr3]);
?>
<table>
<?php foreach($agroup as list($a, $b, $c)){ ?>
<tr>
<td><?php echo $a['name']; ?></td>
<td><?php echo $b['name']; ?></td>
<td><?php echo $c['name']; ?></td>
</tr>
<tr>
<td><?php echo $a['age']; ?></td>
<td><?php echo $b['age']; ?></td>
<td><?php echo $c['age']; ?></td>
</tr>
<tr>
<td><?php echo $a['address']; ?></td>
<td><?php echo $b['address']; ?></td>
<td><?php echo $c['address']; ?></td>
</tr>
<?php } ?>
</table>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was trying to do a school project and the result only shows me 1 data from my MySQL table. Here is the code:
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
$name=$row['Name'];
$startdate=$row['startdate'];
$finaldate=$row['finaldate'];
$within=$row['within'];
$reason=$row['reason'];
$remaining=$row['remaining'];
$status=$row['status'];
$id=$row['id'];
}
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $startdate; ?></td>
<td><?php echo $finaldate; ?></td>
<td><?php echo $within; ?></td>
<td><?php echo $reason; ?></td>
<td><?php echo $remaining; ?></td>
<td><?php echo $status; ?></td>
</tr>
</table>
Can anyone help tell me what did I am missing?
As the other commenters have said, you need to include the output block within the while loop, else you will only get the output from the last row.
Try this:
<table>
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
$name=$row['Name'];
$startdate=$row['startdate'];
$finaldate=$row['finaldate'];
$within=$row['within'];
$reason=$row['reason'];
$remaining=$row['remaining'];
$status=$row['status'];
$id=$row['id'];
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $startdate; ?></td>
<td><?php echo $finaldate; ?></td>
<td><?php echo $within; ?></td>
<td><?php echo $reason; ?></td>
<td><?php echo $remaining; ?></td>
<td><?php echo $status; ?></td>
</tr>
<?php } ?>
</table>
Really all the intermediate variables you're using are redundant, so this is equivalent and much shorter:
<table>
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['startdate']; ?></td>
<td><?php echo $row['finaldate']; ?></td>
<td><?php echo $row['within']; ?></td>
<td><?php echo $row['reason']; ?></td>
<td><?php echo $row['remaining']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php } ?>
</table>
Or more concise still, without the messy switching between html and php modes:
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
echo "<table>";
while($row= mysql_fetch_array($run)){
echo "<tr>";
echo "<td>$row['Name']</td>";
echo "<td>$row['startdate']</td>";
echo "<td>$row['finaldate']</td> ";
echo "<td>$row['within']</td>";
echo "<td>$row['reason']</td>";
echo "<td>$row['remaining']</td>";
echo "<td>$row['status']</td>";
echo "</tr>";
}
echo "</table>";
If you want headers too, you can do as below:
<?php include("connect.php");?>
<table>
<tr>
<th>Name</th>
<th>Start Date</th>
<th>Final Date</th>
<th>Within</th>
<th>Reason</th>
<th>Remaining</th>
<th>Status</th>
</tr>
<?php
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query) or die(mysql_error());
?>
<?php
while($row= mysql_fetch_array($run)){
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['startdate']; ?></td>
<td><?php echo $row['finaldate']; ?></td>
<td><?php echo $row['within']; ?></td>
<td><?php echo $row['reason']; ?></td>
<td><?php echo $row['remaining']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php } ?>
</table>