Unexpected T_WHILE error [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Can anybody point out what I am doing wrong in my code below? I'm getting an "UNEXPECTED T_WHILE" error.
echo '
<table border="1">
<tr>
<td>Events</td>
<td>Category</td>
</tr>
<tr>
<td>', while ($row = mysql_fetch_assoc($queryresult)) { $title = $row['eventTitle']; echo $title; }, '</td>
<td>', while ($row = mysql_fetch_assoc($queryresult)) { $category = $row['eventTitle']; echo $category; }, '</td>
</tr>
</table> ';

Try this
First off, close your PHP tags.
?>
<table border="1">
<tr>
<td>Events</td>
<td>Category</td>
</tr>
<tr>
<td>
<?php
while ($row = mysql_fetch_assoc($queryresult)) {
$title = $row['eventTitle'];
echo $title;
}
?>
</td>
<td>
<?php
while ($row = mysql_fetch_assoc($queryresult)) {
$category = $row['eventTitle'];
echo $category;
}
?>
</td>
</tr>
</table>

PHP -
$array = array();
$i = 0;
while ($row = mysql_fetch_assoc($queryresult))
{
$array[$i]['title'] = $row['eventTitle'];
$array[$i]['category'] = $row['eventCategory'];
$i++;
}
HTML -
<table border="1">
<tr>
<td>Events</td>
<td>Category</td>
</tr>
<?php
foreach($array as $arr)
{
?>
<tr>
<td><?php echo $arr['event']; ?></td>
<td><?php echo $arr['category']; ?></td>
</tr>
<?php
}
?>

Related

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\read.php on line 41 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I want to create a website that can do the basic my SQL functions like create, read, update and delete and I was working on the read page when I got this error:
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\read.php on line 41.
Here is my code :
<?php
$link = mysqli_connect("localhost", "root", "", "db_school");
if (mysqli_connect_error()) {
die ("Database Connection Error");
}
$query = "SELECT * FROM tbl_student ORDER BY id DESC";
$result = mysqli_query($link, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Read</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
</head>
<body>
<?php
include 'procces_read.php';
?>
<div class="container">
<div class="box">
<h4 class="display-4 text-center">Read</h4>
<br>
<?php if (isset($_GET['success'])) { ?>
<div class="alert alert-success" role="alert">
<?php echo $_GET['success']; ?>
</div>
<?php } ?>
<?php if (mysqli_num_rows($result)) { ?>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Student ID</th>
<th scope="col">Student Name</th>
<th scope="col">Student DOB</th>
<th scope="col">Student Sex</th>
<th scope="col">Student Class</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
while($rows = mysqli_fetch_assoc($result)){
$i++;
}
?>
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $rows['student_id'];?></td>
<td><?php echo $rows['Name']; ?></td>
<td><?php echo $rows['DOB']; ?></td>
<td><?php echo $rows['sex']; ?></td>
<td><?php echo $rows['Class']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="link-right">
Create
</div>
</div>
</div>
</body>
</html>
Your table rows (from <tr> to </tr>) should be inside the while loop. The $rows variable is not defined outside of it.
<tbody>
<?php
$i = 0;
while($rows = mysqli_fetch_assoc($result)){
$i++;
?>
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $rows['student_id'];?></td>
<td><?php echo $rows['Name']; ?></td>
<td><?php echo $rows['DOB']; ?></td>
<td><?php echo $rows['sex']; ?></td>
<td><?php echo $rows['Class']; ?></td>
</tr>
<?php }
} ?>

Ordered list for table [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My database data is echoed in a table list but I will like for the list to be ordered for reference purposes. I have tried several approaches but it doesn't seem to work. Any way possible? here is my code
<table width="auto" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
<tr align="center" id="listTableHeader">
<td>S/N</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<?php
while($row = dbFetchAssoc($result)) {
extract($row);
if ($i%2) {
$class = 'row1';
} else {
$class = 'row2';
}
$i += 1;
?>
<tr class="<?php echo $class; ?>">
<td width="70" align="center">Delete</td>
<td></td>
<td><?php echo $Firstame; ?></td>
<td><?php echo $LastName; ?></td>
</tr>
<?php
} // end while
?>
<tr>
<td colspan="5"> </td>
</tr>
<tr>
<td colspan="5" align="right"></td>
</tr>
</table>
I can see some mistakes in your code like disorder of some statements.I don't know what is your actual code.How ever here iI am share with my knowledge through the small example.I think it may be helps to solve your issue.You just look the following code and change it with your tables and database connection credential.If any problem in this,you can tell me.I will give the further solution as I can.
<table width="auto" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
<tr align="center" id="listTableHeader">
<td></td>
<td>S/N</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<?php
$conn= mysqli_connect("localhost", "root", "", "test");
if(!$conn)
{
echo 'not';die;
}
$result=mysqli_query($conn,"select * from animals");
$i=0;
$j=1;
while($row = mysqli_fetch_array($result)) {
$Firstname=$row['name'];
$LastName=$row['animal'];
$id=$row['animal_ID'];
if ($i%2) {
$class = 'row1';
} else {
$class = 'row2';
}
$i += 1;
?>
<tr class="<?php echo $class; ?>">
<td width="70" align="center">Delete</td>
<td><?php echo $j;?></td>
<td><?php echo $Firstname; ?></td>
<td><?php echo $LastName; ?></td>
</tr>
<?php
$j++;
} // end while
?>
<tr>
<td colspan="5"> </td>
</tr>
<tr>
<td colspan="5" align="right"></td>
</tr>
</table>

delete using checkbox code not working properly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Code not working properly always shows the message fail.
function delete(){
$con = mysqli_connect("localhost","root","","rishita_db");
$sql="select * from 14_patientdetails";
$result=mysqli_query($con,$sql);
?>
<form method="post" action="">
<center>
<h1><u>Patient Details</u></h1>
<table border="1" style="font-family:Georgia;color:#800000;font-style:bold;">
<tr style="font-family:Georgia;color:green;font-style:bold;">
<th>#</th>
<th>Patient ID</th>
<th>Patient Name</th>
<th>DOB</th>
<th>Gender</th>
<th>Address</th>
<th>Phone No.</th>
<th>Medicare</th>
<th>Doctor Associated</th>
</tr>
<form method="post" action="">
<?php
while($row=mysqli_fetch_array($result))
{
$r=$row['patientId'];
?>
<tr>
<td><input type='checkbox' name='checkbox[]' id="checkbox" value=<?php echo $r; ?>></td>
<td><?php echo $row['patientId']; ?></td>
<td><?php echo $row['patientName']; ?></td>
<td><?php echo $row['DOB']; ?></td>
<td><?php echo $row['Gender']; ?></td>
<td><?php echo $row['Address']; ?></td>
<td><?php echo $row['Phone']; ?></td>
<td><?php echo $row['Medicare']; ?></td>
<td><?php echo $row['Doctor']; ?></td>
</tr>
<?php
}
?>
</table>
<table>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="del" type="submit" id="del" value="Delete"></td>
</tr>
</table>
</form>
<?php
if(isset($_POST["del"]))
{
if(isset($_POST["checkbox"]))
echo 'Enter';
$chk = isset($_POST['checkbox']);
$chkcount = count($chk);
for($i=0;$i<$chkcount;$i++){
$del=$chk[$i];
$sql1 = "DELETE FROM 14_patientdetails WHERE id='$del'";
$q = mysqli_query($con,$sql1);
}
if($q){
echo "Success";
}else{
echo 'Fail';
}
}
}
This is wrong:
$chk = isset($_POST['checkbox']);
$chkcount = count($chk);
for($i=0;$i<$chkcount;$i++){
$del=$chk[$i];
Now there are one too many mistakes, multiple <form> tags and a single closing </form>
I couldn't not edit your code on my phone so I will suggest the way to get along. I'm pretty sure you'll love it when you do it yourself.
Make a <td><input type = "checkbox" name = "checkbox" value = "<?php echo $r; ?>"/>Proceed</td> inside a while-loop something similar to your code while($row = mysqli_fetch_array($result))
In the end of the <form> make a <button> and redirect user to another page probably delete.php. Now, check its set;
if(isset($_POST['checkbox']
{
//foreach loop for your query
foreach($_POST['checkbox'] as $val)
{
// check what you're getting..
echo $val;
}}
You could also use a simple for-loop
If you want:
for($i = 0; $i <count($_POST['checkbox']; $i++)
{
// do your stuff..
}
Bottom line: try to differentiate your forms, queries and make a single loop to read from database and assign the values at the same time to your checkboxes.
This is what I have understood and tried to write from my phone,
please study mysqli/PDO to prevent SQL Injection/XSS.

html php not properly aligning for each column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I m trying to run two loops the first query in retrieves the column .The second query takes the primary id of the first table and executes the query.
Final result should be like given in the image
Im not able to give the proper alignment
Below is the code
<table width="100%" border="1" cellspacing="1" cellpadding="1" >
<tr>
<td>Col1</td>
<td>col2</td>
<td>col3</td>
<td>col4/td>
</tr>
<?php
$mast = mysql_query("select * from table1 where av_master_id='".$_REQUEST['id']."'");
while($res_mas= mysql_fetch_assoc($mast))
{
?>
<tr>
<td><?php echo $res_mas['col1'];?></td>
<?php
$room= mysql_query("SELECT * FROM `table2` WHERE av_room_id='".$res_mas['av_room_id']."'");
while($res_room= mysql_fetch_assoc($room))
{
?>
<td><?php echo $res_room['col2'];?></td>
<td><?php echo $res_room['col3'];?></td>
<td><?php echo $res_room['col4'];?></td>
</td>
</tr><tr>
<?php }?>
</tr>
<?php } ?>
</table>
This should do it:
<table width="100%" border="1" cellspacing="1" cellpadding="1" >
<tr>
<td>Col1</td>
<td>col2</td>
<td>col3</td>
<td>col4/td>
</tr>
<?php
$mast = mysql_query("select * from table1 where av_master_id='".$_REQUEST['id']."'");
while($res_mas = mysql_fetch_assoc($mast)) {
$room = mysql_query("SELECT * FROM `table2` WHERE av_room_id='".$res_mas['av_room_id']."'");
$count = 0;
while($res_room= mysql_fetch_assoc($room)) {
$count += 1;
?>
<tr>
<td><?php echo $count === 1 ? $res_mas['col1'] : "";?></td>
<td><?php echo $res_room['col2'];?></td>
<td><?php echo $res_room['col3'];?></td>
<td><?php echo $res_room['col4'];?></td>
</tr>
<?php } } ?>
</table>
(untested)

Column wise sum in PHP from HTML table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to sum of a column
<table width="240" border="1">
<tr>
<td width="94">Name</td>
<td width="130">Current Amount</td>
</tr>
<?php
do {
?>
<tr>
<td><?php echo $row_Record['name']; ?></td>
<td><?php echo $row_Record['current_amount']; ?></td>
</tr>
<?php
}
while ($row_Record = mysql_fetch_assoc($Record));
?>
<tr>
<td colspan="2">Total = </td>
</tr>
</table>
I don't know how to do this kind of sum.
Please any one help me.
Thank you.
By using Jquery, you can do something like that
$(document).ready(function(){
var sum = 0
$(".sum").each(function(){
sum += parseFloat($(this).text());
});
alert(sum);
});
Jsfiddle
Alternatively, you can sum all the values by using PHP when fetching the values.
Something like that:
$sum = 0;
while($read = mysqli_fetch_array()){
$sum += $read['row'];
}
echo $sum; //total
<table width="240" border="1">
<tr>
<td width="94">Nmae</td>
<td width="130">Current Amount</td>
</tr>
<?php
$tmp=0;
do {
$tmp+=$row_Record['current_amount'];
?>
<tr>
<td><?php echo $row_Record['name']; ?></td>
<td><?php echo $row_Record['current_amount']; ?></td>
</tr>
<?php } while ($row_Record = mysql_fetch_assoc($Record)); ?>
<tr>
<td colspan="2">Total = <?php echo $tmp; ?></td>
</tr>
</table>

Categories