I have a table which displays information from a database. I added a column where I want to display a message according to row number displayed.
<form name="afisare1" method="POST" >
<input type="submit" name="opt1" value="OK"/>
<?php
if (isset($_POST['opt1'])) {
$loc=mysql_query("SELECT loc FROM program WHERE den='Option'");
//$loc result is a number
$sql=mysql_query("SELECT Col1, Col2
FROM date WHERE Opt_1='Option' OR Opt_2='Option'
ORDER BY Col2 DESC");
?>
<table>
<thead>
<tr>
<th>Col1</th> <th>Col2</th> <th>OK/NOT OK</th>
</tr>
</thead>
<?php
$num_rows = 0;
while ($row = mysql_fetch_assoc($sql)) {
$num_rows++;
if ($num_rows <= $loc) {
echo"<tr>
<td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td>
</tr>";
break;
}
if ($num_rows > $locuri_buget) {
//here i have a problem because i don't know how to display something like this :
//echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>NOT OK</td></tr>";
}
}
} ?>
</table>
</form>
For example if the result of $loc=2 i want to echo for the first 2 rows OK and for extra rows i want to echo NOT OK
To retrieve loc from MySQL, your original code does it like this:
$loc = mysql_query("SELECT loc FROM program WHERE den='Option'");
But you might want to change it like this:
$loc = 0; # default value in case of error in a query
$result = mysql_query("SELECT loc FROM program WHERE den='Option'");
while($row = mysql_fetch_assoc($result)){
$loc = $row['loc'];
}
Or if you want retrieve only one record from your databse, you can also write a SQL with LIMIT like this:
$loc = 0; # default value in case of error in a query
$result = mysql_query("SELECT loc FROM program WHERE den='Option' LIMIT 1");
if($row = mysql_fetch_assoc($result)){
$loc = $row['loc'];
}
Hope this helps.
You can try with following inside while loop
if($num_rows<=$loc){
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}else{
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>NOT OK</td></tr>";
}
$num_rows = 0;
while($row = mysql_fetch_assoc($sql)){
$num_rows++;
if($num_rows<=$loc){
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}else {
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}
}
I think your problem is with break,
break ends execution of the current for, foreach, while, do-while or switch structure.
Break
Related
I executed this code in localhost but it kept looping the table infinitely. How do I fix this? Is there something I should add? The looping never stops until I click the x button to stop loading the page.
//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$query=("SELECT * FROM songs ORDER BY songid DESC");
$result = mysql_query($query) or die(mysql_error());
?>
<html>
<head>
<title>View</title>
</head>
<body>
Add New Data<br/><br/>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Song ID</td>
<td>Title</td>
<td>Artist</td>
<td>Genre</td>
<td>Language</td>
<td>Lyrics</td>
<td>Updated by</td>
</tr>
<?php
$counter == 1;
$res = mysql_fetch_array($result);
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
do {
echo "<tr>";
echo "<td>".$res['songid']."</td>";
echo "<td>".$res['title']."</td>";
echo "<td>".$res['artist']."</td>";
echo "<td>".$res['genre']."</td>";
echo "<td>".$res['language']."</td>";
echo "<td>".$res['lyrics']."</td>";
echo "<td>".$res['update']."</td>";
echo "<td>Edit | Delete</td>";
} while($res)
?>
</table>
You are missing $res = mysql_fetch_array($result); at the end of the loop. Currently, the while statements checks if $res is (still) truthy, but it never changes, so it will always evaluate to TRUE (and so, the loop will always continue to run).
I was just about to write the same thing as Daan Meijer. res never changes and so its always true for the while-statement. I just want to add that mysql_fetch_assoc($result) works a littlebit faster than mysql_fetch_array($result). So if you have a big database, mysql_fetch_assoc($result) is the better choice.
i have an search box on website but whenever we search, it doesn't give any output.
I want to search for page title from database. But don't know what is wrong.
<div id="siteSearch">
<h3>Site Search</h3>
<?php
if (isset($_POST['search'])) {
$search = $_POST['search'];
$query = "SELECT * FROM pages WHERE ptitle LIKE '%$search%'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
if ($count > 0) {
while ($fetch = mysql_fetch_array($result)) {
echo $fetch['ptitle'];
}
}
} else {
echo "No result found!";
}
?>
</div>
You are using the semicolon after the while loop:
while($fetch = mysql_fetch_array($result));
Please remove the semicolon in this line.
You have a misplaced closing bracket. The one above else should be 5 lines below. As it currently stands, the else clause relates to if (isset($_POST['search'])).
I have a database (SQL) with the table "Staff" with two records in it. I am required to display the contents of these records on a web page using PHP.
<html>
<head>
<title>CES Staff details</title>
</head>
<body>
**code emitted**
<?php
$row = mysql_fetch_array($result) ;
$looping = 1;
$i = $row.length;
while ($looping <= $i)
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
$looping++;
}
?>
</body>
</html>
How would I change the while loop correctly so that it will display both records on the page.
Thanks!
mysql_fetch_array() only retrieves a single row from the database. You need to call it inside your while loop to retrieve all rows. The $looping increment is unnecessary here, since mysql_fetch_array() returns false when no more rows are available.
while ($row = mysql_fetch_array($result))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
I'll do...
while ($row = mysql_fetch_assoc($result)) {
// print $row;
}
while ($row = mysql_fetch_array($result))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
<?php
$qry = mysql_query($result);
while ($row = mysql_fetch_array($qry))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
?>
PHP has great documentation with examples. Check out the example for mysql_fetch_array().
Your code should look like this:
<?php
while($row = mysql_fetch_array($result)) {
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
?>
Use this
while($row = mysql_fetch_array($result))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
In php there is no such thing like $row.length; the "." is an operator for string concatenation. Read more on mysql_fetch_array at http://php.net/manual/en/function.mysql-fetch-array.php.
<?php
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
?>
I have looked through similar problems and solution but somehow only half way help me with my problem. I'm trying to make a form to checked more than one record from MySQL database and display the checked record to another page. Somehow I managed to do the page with check boxes but I don't know how to display the record checked. It can only display the first row of the record or all the records regardless which box are checked.
This is checkbox page
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
{
$rows[] = $row['IllNo'];
}
foreach($rows as $value);
echo "";
echo " ";
echo $row['IllNo'];
echo "";
}
echo "";
?>
This is display record checked
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$rows[]=$row['IllNo'];
foreach($rows as $value);
if ($rows= 'checked') {
echo "";
echo $value;
}
Any help are welcome. Thank you.
There's actually a lot of problems with that script including syntax errors, calling the wrong variable name, form not opening where it should, invoking PHP after you already have, etc...
To get a good answer to you, you should share what make $row['IllNo'] should equal to indicate if it should be checked or not.
I reformatted it a bit and this may give you a good start.
<form NAME ="form1" METHOD ="POST" ACTION ="dari.php">
<table>
<?php
$columns = count($fieldarray);
//run the query
$result = mysql_query("SELECT * FROM request_item ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error()) ;
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
echo "<tr><td>";
echo "<Input type = 'Checkbox' Name ='ch1' value ='ch1'";
// check checked if it is. this will be checked if $row['IllNo'] has a value
// if there were a condition to make it checked, you would put the condition
// before the ?
echo $row['IllNo'] ? ' checked' : '';
echo ' />';
echo $row['IllNo'];
echo "</td></tr>";
}
?>
</table>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Choose your books">
</FORM>
I have a code that I have used over and over again before and now it's messing up. All I want to do is list information from the database into the table on the page, but now it will only show one result, instead of all the results it has found.
<table>
<tr><td style="background-color:#009745; color:#FFFFFF"><center><strong>Address Book</strong></center></td></tr>
<tr>
<?php
$getids = mysql_query("SELECT id, first_name, last_name FROM accounts WHERE s1='$id' ORDER BY id DESC", $db);
if (mysql_num_rows($getids) > 0) {
while ($gids = mysql_fetch_array($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
}
?>
<td><?= $ab_id ?> - <?= $ab_fn . " " . $ab_ln ?></td>
<?php
} else {
?>
<td><center>No Contacts</center></td>
<?php
}
?>
</tr>
</table>
please help me with this.
Thank You for your help :)
I love this site!! I can always get answers when I need them.
I saw two thing wrong
you are using mysql_fetch_array and later you are using string indexes to print the result
print the things in loop it is overriding values and just storing last row
if (mysql_num_rows($getids) > 0) {
while ($gids = mysql_fetch_assoc($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
echo '<td>'.$ab_id.' -'. $ab_fn.''.$ab_ln.' </td>';
}
In this messy code you're closing the while loop too early:
while ($gids = mysql_fetch_array($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
}
Only the last retrieved row is used later on. Also, don't use mysql_fetch_array if you're not accessing the numeric indeces of your result. Use mysql_fetch_assoc instead.