while staement inside another - php

.I have the following code. I am wondering why does my "echo '<font color="#FF0000">'.$update_date2.'</font><br><br>'.$status.'<br>';" does not show on the screen?
.is it okay to use while statement inside another while statement? as shown in my code below.
<?php
$getquery = mysql_query("SELECT * FROM it_task ORDER BY task_id DESC");
while ($rows = mysql_fetch_array($getquery))
{
$id= $rows['task_id'];
$date=$rows['date'];
$update_date = $rows['update_date'];
$project=$rows['project'];
$topic=$rows['topic'];
$instby=$rows['instby'];
$inst=$rows['inst'];
$dline=$rows['dline'];
$ocome=$rows['ocome'];
$comm=$rows['comm'];
$fin=$rows['fin'];
echo "<div id=\"container\">";
echo "<table>";
echo "<tr>";
echo "<div id=\"conid\">$id</div>";
echo "<div id=\"condate\">$date</div>";
echo "<div id=\"conproject\">$project</div>";
echo "<div id=\"contask\">$topic</div>";
echo "<div id=\"conselect\">$instby</div>";
echo "<div id=\"conselect1\">$inst</div>";
echo "<div id=\"condline\">$dline</div>";
echo "<div id=\"conocome\">";
$updatesquery = "SELECT * FROM it_task_update WHERE update_id=$id";
while($rows1 = mysql_fetch_array($updatesquery));
{
$update_date2 = $rows1['update_date'];
$status = $rows1['ocome'];
$update_id = $rows1['update_id'];
echo '<font color="#FF0000">'.$update_date2.'</font><br><br>'.$status.'<br>';
}
echo "</div>";
echo "<div id=\"concomm\">$comm</div>";
echo "<div id=\"confin\">$fin</div>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
?>
.where have i done wrong in this set of codes. can anyone guide me pls. TIA! More power! :)

$updatesquery = "SELECT * FROM it_task_update WHERE update_id=$id";
<---dude, where's my query?
while($rows1 = mysql_fetch_assoc($updatesquery));
You're not actually executing the query. You're trying to perform a fetch on a string, which is an error condition in MySQL, causing the fetch call to return FALSE, which makes the while() loop terminate immediately..
in other words, you're missing a call to mysql_query() in between those lines.
on another note, if the inner loop is going to be executed "many" times, you should look at rewiting both queries as a single one with a JOIN clause. It's almost always after to do a single large query then run multiple smaller ones.

Do you have any results from SELECT * FROM it_task_update WHERE update_id = $id?
Try doing
print_r($rows1); before line $update_date2 = $rows1['update_date'];
and view the results.

You have a semicolon after the inner while
while($rows1 = mysql_fetch_assoc($updatesquery));
{
// stuff
}
should be
while($rows1 = mysql_fetch_assoc($updatesquery))
{
// stuff
}

You're trying to retrieve results inside processing your query from a query you never execute. Notice that you called mysql_query before you started looping through your results. Then notice you tried to start fetching results from a query you never executed inside your inner while loop.

Yes, it is OK, to have a while or another type of loop inside another one, because it depends on your business logic. You have forgotten to execute the second query before the while loop, though. It would be better if you did this with an INNER JOIN or something so you don't waste the resources.
Also, I would suggest you not to print out the DOM elements with echo, but have them there like this <div id="username">Username: <?php echo $username; ?></div>. However, even in your case there is place for keeping your code clean. Here is my suggestion:
<?php
$getquery = mysql_query("SELECT * FROM it_task ORDER BY task_id DESC");
while ($rows = mysql_fetch_array($getquery))
{
$id= $rows['task_id'];
$date=$rows['date'];
$update_date = $rows['update_date'];
$project=$rows['project'];
$topic=$rows['topic'];
$instby=$rows['instby'];
$inst=$rows['inst'];
$dline=$rows['dline'];
$ocome=$rows['ocome'];
$comm=$rows['comm'];
$fin=$rows['fin'];
echo '<div id="container\">'.
'<table>'.
'<tr>'.
"<div id=\"conid\">$id</div>".
"<div id=\"condate\">$date</div>".
"<div id=\"conproject\">$project</div>".
"<div id=\"contask\">$topic</div>".
"<div id=\"conselect\">$instby</div>".
"<div id=\"conselect1\">$inst</div>".
"<div id=\"condline\">$dline</div>".
"<div id=\"conocome\">";
$updatesquery = "SELECT * FROM it_task_update WHERE update_id=$id";
// you need to execute the query
$updatesresult = mysql_query($updatesquery);
while($rows1 = mysql_fetch_assoc($updatesresult)) // <-- no semicolon here
{
$update_date2 = $rows1['update_date'];
$status = $rows1['ocome'];
$update_id = $rows1['update_id'];
echo '<font color="#FF0000">'.$update_date2.'</font><br><br>'.$status.'<br>';
}
echo '</div>'.
"<div id=\"concomm\">$comm</div>".
"<div id=\"confin\">$fin</div>".
'</tr>'.
'</table>'.
'</div>';
}
?>
Update
Your inner while statements should not be terminated with a semicolon at the logic point. Please check the code above to see where I have placed the comment for you.

Related

If while loop doesn't show first element

I'm really stuck on this guys, and I know where the fault is but I can't seem to fix it.
The while loop itself works it's the if that is causing all the trouble.
Whenever there are 3 groups in the database it only displays 2 the strange part it will only display the items in the while loop. But the first item is somehow tied to the if statement.
public function get_group($user_id){
$sql3="SELECT * FROM groups WHERE user =
$user_id";
$results = mysqli_query($this->db, $sql3);
$user_data = mysqli_fetch_array($results);
if ($results->num_rows > 0) {
// output data of each row
while($row = $results->fetch_assoc()){
echo "hello";
echo "group :" . $row["group"] . "<br>";
echo "groupdesc:" . $row["groupdesc"] . "<br>";
echo "<a class='btn btn-primary' href='project.php?groupid=" . $row["groupid"] . "'>></a>";
}
}
else {
echo "0 results";
}
}
It seems it can't output the first one the first one just keeps hidden.
You fetch your first row in this line:
$user_data = mysqli_fetch_array($results);
And you do nothing with it.
And please do not mix procedural and object-oriented usage of mysqli.

Running "SQL" query for each comment box will speed down multi-level commenting system?

i am developing multi-level (comments and repelis) comment system using php.
all comments and repelis are in same database table.
in the function this system have to run 'sql' query for each comment box to generate them in correct order.
so, will this slow down strongly my system while handling thousands of comments.
here below is my function,
function getComments($row) {
echo "<li class='comment'>";
echo "<div class='aut'>".$row['author']."</div>";
echo "<div class='comment-body'>".$row['comment']."</div>";
echo "<div class='timestamp'>".$row['created_at']."</div>";
echo "<a href='#comment_form' class='reply' id='".$row['id']."'>Reply</a>";
$q = "SELECT * FROM threaded_comments WHERE parent_id = ".$row['id']."";
$r = mysql_query($q);
if(mysql_num_rows($r)>0)
{
echo "<ul>";
while($row = mysql_fetch_assoc($r)) {
getComments($row);
}
echo "</ul>";
}
echo "</li>";
}
or i have another idea to put comments and repelis in tow tables. while comments displaying,
get all repelis data to Array and push them in to related comment box using for loop for that array.
which method will be best for this work. the best performance ?

Get line breaks in the my-sql_fetch_array table results

I have a simple project that I am working on and I'm having a hard time finding the code I need to get line breaks in the following table:
<?php
// connect to the database
$host = '###';
$username = '###';
$pass = '####';
mysql_connect($host,$username,$pass) or die(mysql_error());
mysql_select_db("####") or die(mysql_error());
// select everything from the table
$query = "SELECT * FROM Employees";
$result = mysql_query($query) or die(mysql_error());
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
mysql_close();
?>
Everything works correctly and it grabs data from the correct database and table. But when it displays results it is all on the same line ("record1record2record3") and I'd like a line break between employee records.
I've searched this question and it seems like my results all show me an entirely different way of doing this. I've already got the code written and fussed with it a lot to get it working. Can I just make a simple alteration to the above code to get the breaks I want?
The <tr> tags must also be inside the while loop so that they are outputted for each row. They make the rows in the HTML table.
while( ($row = mysql_fetch_array($result)))
{
echo "<tr>";
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
echo "</tr>";
}

PHP syntax help on if statement

I'm trying to determine the right syntax from what I'm trying to do with MySQL. I'm basically saying that if a value in a certain column of a row of a table is equal to some session variables, I want to echo out info.
I have a table with subject, description and user. User is set by taking the current user's first name and last name and inserting it into the table under user. This is done by the following code:
$sql="INSERT INTO tbl_name (subject, description, user)
VALUES
('$_POST[subject]','$_POST[description]','$_SESSION[firstname] $_SESSION[lastname]')";
Then once I'm calling this data back out, I want to basically allow the user to delete content that they submitted themselves. The first step for me is to be able to display it in the table. I believe this is just syntax error, but I've confused myself now with how things are set up:
$sql = "SELECT * FROM tbl_name ORDER BY subject, description
LIMIT {$startpoint},{$limit}";
$result = $mysqli->query($sql);
$num_rows = mysqli_num_rows($result);
if($num_rows>0){
$field_num = $mysqli->field_count;
echo "<h1>HERE ARE SOME EXAMPLES:</h1>";
echo "<table border='0'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->subject}</td>";
echo "<td>{$field->description}</td>";
echo "<td>{$field->user}</td>";
if('$field->user' == '$_SESSION[firstname] $_SESSION[lastname]'){
echo '<td>You can delete this</td>';
}
}
I figured the $field->user would equal $_SESSION[firstname] $_SESSION[lastname] because that's how it was initially submitted to the table (without the '.' for concatenation).
Any help would be appreciated. Thanks!
EDIT
Here's the result of my table output code. The results are actually being display with $cell instead of from within the for loop I believe. I've added the if statement in after the but it doesn't seem to recognize echo "<td>".$field->user."</td>"; which makes me think that that is where the problem lies. What I would like to do is be able to add the if statement in a immediately after `echo {$field->user}"; to keep the code clean. I think I've confused myself thoroughly:
if($num_rows>0){
$field_num = $mysqli->field_count;
echo "<h1>HERE ARE SOME JOBS:</h1>";
echo "<table border='0'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->subject}</td>";
echo "<td>{$field->description}</td>";
echo "<td>{$field->user}</td>";
if($field->user == $_SESSION[firstname]." ".$_SESSION[lastname]){
echo '<td>You can delete this</td>';
}
else{
echo "<td>".$field->user."</td>";
echo "<td>".$_SESSION[firstname]." ".$_SESSION[lastname]."</td>";
}
}
echo "</tr>\n";
while($row = mysqli_fetch_row($result))
{
echo"<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysqli_free_result($result);
}
else{
echo 'There are no jobs!';
}
I re-wrote the code in a way that was a little bit easier for me to understand (though maybe not the shortest way to do it):
while($row = mysqli_fetch_array($result))
{
echo"<tr>";
echo"<td>".$row['subject']."</td>";
echo"<td>".$row['description']."</td>";
echo"<td>".$row['user']."</td>";
if($row['user'] == $_SESSION['firstname']." ".$_SESSION['lastname']){
echo"<td>You can delete this</td>";
}
else{
echo"<td>Code didn't work</td>";
}
echo "</tr>\n";
}
It ended up working this way. If there's way to do this shorter then feel free to post it here otherwise thanks for the help!

loop through database and show in table

I am trying to loop though my users database to show each username in the table in their own row. I can only get it to show one user but loops through this the number of rows there are. Code is below
<?php
require_once ('../login/connection.php');
include ('functions.php');
$query = "SELECT * FROM users";
$results=mysql_query($query);
$row_count=mysql_num_rows($results);
$row_users = mysql_fetch_array($results);
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
?>
Thanks
mysql_fetch_array fetches a single row - you typically use it in a while loop to eat all the rows in the result set, e.g.
echo "<table>";
while ($row_users = mysql_fetch_array($results)) {
//output a row here
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You're only fetching one row:
$row_users = mysql_fetch_array($results);
You're never calling a fetch again so you're not really looping through anything.
I'd suggest changing your loop to the following:
echo "<table>";
while ($row = mysql_fetch_array($results)) {
echo "<tr><td>".($row['email'])."</td></tr>";
}
echo "</table>";
The while loop will loop through the results and assign a row to $row, until you run out of rows. Plus no need to deal with getting the count of results at that point. This is the "usual" way to loop through results from a DB in php.
In the new MYSQLI, I use this coding
$query = "SELECT * FROM users";
$results=mysqli_query($con,$query);
$row_count=mysqli_num_rows($results);
echo "<table>";
while ($row = mysqli_fetch_array($results)) {
echo "<tr><td>".($row['id'])."</td></tr>";
}
echo "</table>";
mysqli_query($con,$query);
mysqli_close($con);
Your problem is in this line:
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
You're echoing out a <table> tag again. Remove that so your code looks like this:
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You don't need to output a table within a table the way you're doing it.
$result = mysql_query("SELECT `email` FROM `users`");
$num_emails = mysql_num_rows($result);
echo "<table><caption>There are/is $num_emails email(s)</caption>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>{$row['email']}</td></tr>";
}
echo '</table>';
Something like that should work.

Categories