Echo items with value of three - php

I need to echo all items in my column that have the value of three. If a field has the value of three I then need to echo the 'name' and 'description' on that row.
This is what I have so far
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo $row['status'];
}
I need to write 'if $row[`statusĀ“ == 3 echo 'description' 'name' else echo 'no current staus'
I hope I made some sense because I am seriously confused

You can actually select the result which only has 3 value in its status
for that use this
$query = "SELECT * FROM yourtable WHERE status='3'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array(result)) {
echo "Name: ".$row['name']."<br>Description: ".$row['description'];
}
and if you want to parse you result to display only those of value this then use this
$query = "SELECT * FROM yourtable";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array(result)) {
if($row['status']==3) {
echo "Name: ".$row['name']."<br>Description: ".$row['description'];
}
}

if $row['status'] == 3 {
echo ($row['description']." ".$row['name']);
}
else
{
echo ('no current staus');
}
The "." in the first echo means string concatenation. I'm separating description and name with a single space character, feel free to change this.

I'm expecting something more since you have the "i need to write" part that is the answer?
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) // fetch_assoc better for your case
{
if( $row['status'] == 3 ) {
echo $row['description'];
}else {
echo 'no current staus';
}
}
For the sake of info for tasks with a numeric status
switch( $row['status'] ) {
case 0:
// do something
break;
case 1:
// do something
break;
case 2:
// do something
break;
case 3:
// do something
break;
default:
echo 'No status set';
}

if ($row['status'] == 3) {
echo $row['name'];
echo $row['description'];
}

$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
if ($row['status'] == 3) {
echo $row['name'] . ' - ' . $row['description'];
}
else {
echo 'no curent status';
}
}

if($row['status']==3) {
echo $row['description'];
echo $row['name'];
} else {
echo 'no current status';
}
Maybe you should learn about basics of PHP and algorithms and make sure you understand this, before you move on.

This?
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
if (!in_array(3, $row['status']))
{
echo 'no current staus';
break;
}
echo $row['description'] . <br />;
echo $row['name'] . <br />;
}

or you could use SQL to select the values for you...
$query1 = SELECT * FROM table WHERE column = '3';
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo $row['status'];
}

Related

How to access while array outside the loop in PHP

I want to access while array $row2 outside the loop so that I can compare in if else condition, because $row2 array contains many number. Is there any solution make $row array accessible outside the loop or any other method?
<?php
$sql = (" SELECT * FROM result ");
$query = mysqli_query($db, $sql);
while($row = mysqli_fetch_array($query)) {
$row2 $row['id'];
}
for($i=1;$i<=10;$i++) {
if ($i<= $row2) {
echo "<font color='red'><font size='50px'>".$i."</font></font>"."<br>";
continue;
} else {
echo $i.'<br>';
}
}
?>
If you want to loop over two dependent things you need to nest them or else $row2 will always contain the last value in the while loop:
<?php
$sql = (" SELECT * FROM result ");
$query = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($query)) {
$row2 = $row['id']; // I added an = sign here.
for($i=1;$i<=10;$i++) {
if ($i<= $row2) {
echo "<font color='red'><font size='50px'>".$i."</font></font>"."<br>";
continue;
} else {
echo $i.'<br>';
}
}
}
If this isn't what you want please clarify your question.
Pass your variable to a separate function that performs whatever task you want.
<?php
while($row = mysqli_fetch_array($query))
{
MyFunction($row2);
}
function MyFunction($passed_row2_value){
// perform whatever task you want here.
}
?>
This shows with some format the ids retrieved by the query and fills the blanks without format.
<?php
$sql = (" SELECT * FROM result ");
$query = mysqli_query($db, $sql);
$row2 = [];
while($row = mysqli_fetch_array($query)) {
$row2[$row['id']] = true;
}
for($i=1;$i<=10;$i++) {
if ($row2[$i]) {
echo "<font color='red'><font size='50px'>".$i."</font></font>"."<br>";
} else {
echo $i.'<br>';
}
}
?>

Single row repeating instead of displaying

I'm pretty new at PHP/MySQL, so please be patient with me.
I am trying to get a list of members in a table to show up on a page. Right now it's showing the first member about 10 times and not displaying anyone else's name. I DID have it working, but I don't know what happened. I just want it to display everyone's name once. Here is my code:
<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching];
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
$row2 = mysql_fetch_array($select2);
$student=$row2[student_name];
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
}
else {
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
$row3 = mysql_fetch_array($select3);
while($row2 = mysql_fetch_array($select2)) {
$house=$row3[house];
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>"; }
?>
I miss look on your code, since it is mess, but disregard the mysqli and mysql thing, you want to show how many student in the teacher's classes.
<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching]; <--- This only get first row of the course, if you want multiple course under same username, you need to loop it.
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
$row2 = mysql_fetch_array($select2);
$student=$row2[student_name]; <----- This only get the first row of the student name, if you want multiple student under a course, you need to loop it.
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
}
else {
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
$row3 = mysql_fetch_array($select3);
while($row2 = mysql_fetch_array($select2)) {
$house=$row3[house]; <----This only show the first row of $house under same student, so you need to loop it too.
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>"; }
?>
So what you really want to do is
<?php
$select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$rows = mysql_num_rows($select);
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
while( $row = mysqli_fetch_array( $select ) ) {
$teaching = $row[teaching];
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
} else {
while( $row2 = mysql_fetch_array($select2) ) {
$student=$row2[student_name];
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
while($row3 = mysql_fetch_array($select3)) {
$house=$row3[house];
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>";
}
} // END ELSE
}
} // END ELSE
?>

What is wrong with this pagination?

What is wrong with this code? When I click on 'Next' button it shows empty page and broke my web design? I've trying to figure it out all day but no luck.
<?php
if(isset($_GET['joke_id'])){
$joke_id = $_GET['joke_id'];
$qry = "SELECT * FROM joke WHERE joke_cat = '$joke_id'";
$result = mysqli_query($con, $qry) or die("Query failed: " . mysqli_errno($con));
$line = mysqli_fetch_array($result, MYSQL_BOTH);
if (!$line) echo '';
$previd = -1;
$currid = $line[0];
if (isset($_GET['id'])) {
$previous_ids = array();
do {
$previous_ids[] = $line[0];
$currid = $line[0];
if ($currid == $_GET['id']) break;
$previd = end($previous_ids);
$line = mysqli_fetch_array($result, MYSQL_BOTH);
} while ($line);
}
if ($line) {
echo "<div id=\"box\">";
echo nl2br($line['text']) . "<br /><br />";
echo "</div>";
}
else echo 'Is empty<br/>'; **<------ HERE**
if ($previd > -1)
echo '<span>Prev</span>';
echo str_repeat(' ', 5);
$line = mysqli_fetch_array($result, MYSQL_BOTH);
$query = "select * from joke WHERE joke_cat = '$joke_id' order by RAND() LIMIT 1";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){
echo 'Random';
}
echo str_repeat(' ', 5);
if ($line) echo '<span>Next</span><br /><br />';
echo "</div>\r";
}
?>
Also on the line else echo 'Is empty<br/>'; doesn't matter if there is something .. always shows me 'Is empty'..
UPDATE:
databases are:
joke
id
text
date
joke_cat
and cat_joke is
joke_id
joke_name
change this line:
echo 'Random';
To: not ? after cat_id='.$joke_id.' you need to change &
echo 'Random';
Note: also change next and prev code also...
Your last assigment to $line is null .Therefore you should check in your if not $line but the $previous_ids[] or $currid or the others.

how do i display this columns side by side

im having some problem here. basically, i want to compare columns. so i fetched object and the comparing results appeared just as expected. however, it does not return the compare value anymore after i added the fetch_array to view the current table hoping that the compare value would appear beside the compare value. is there any way i could run the compare code and make it appear the table? i tried a query but it would only work in MySQL and not PHP.
$query = "SELECT * FROM system_audit"; $result = mysql_query($query) or die(mysql_error());
echo " ID Type Setting Value ";
while($row = mysql_fetch_array($result)) {
echo $row['ID'];
echo $row['Type'];
echo $row['Setting'];
echo $row['Value'];
}
while ($row = mysql_fetch_object($result)) {
if($row->Setting != $row->Value) {
echo "X";
} else {
echo "O";
}
}
Your code contains a lot of echo's that have no use. I would suggest learning PHP a bit more.
Your compare is wrong, this should work :
$query = "SELECT * FROM system_audit";
$result = mysql_query($query) or die(mysql_error());
echo " ID Type Setting Value ";
while($row = mysql_fetch_array($result)) {
echo $row['ID'] . "<br>";
echo $row['Type'] . "<br>";
echo $row['Setting'] . "<br>";
echo $row['Value'] . "<br>";
if($row['Setting'] != $row['Value']) {
echo "X" . "<br>";
}
else {
echo "O" . "<br>";
}
echo "<br>";

this program strip would output a searched directory but if not found must output a message

this program must output a directory that you searched for how ever if its not found a message must appear that the org_name is not found,i don't know how to do that, i keep trying on some if-else but it just won't output it.
<?php
$con=mysql_connect("localhost","root","");
if(!$con) {
die('could not connect:'.mysql_error());
}
mysql_select_db("final?orgdocs",$con);
$org_name = $_POST["org_name"];
$position = $_POST["position"];
$result = mysql_query("SELECT * FROM directory WHERE org_name = '$org_name' OR position = '$position' ORDER BY org_name");
echo '<TABLE BORDER = "1">';
$result1 = $result;
echo '<TR>'.'<TD>'.'Name'.'</TD>'.'<TD>'.'Organization Name'.'</TD>'.'<TD>'.'Position'.'</TD>'.'<TD>'.'Cell Number'.'</TD>'.'<TD>'.'Email-Add'.'</TD>';
echo '</TR>';
while ( $row = mysql_fetch_array($result1) ){
echo '<TR>'.'<TD>'.$row['name'].'</TD>'.'<TD>'.$row['org_name'].'</TD>';
echo '<TD>'.$row['position'].'</TD>'.'<TD>'.$row['cell_num'].'</TD>'.'<TD>'.$row['email_add'].'</TD>';
echo '</TR>';
}
echo '</TABLE>';
?>
What you want is mysql_num_rows to check your query for how many result rows it has.
http://de2.php.net/manual/en/function.mysql-num-rows.php
if (mysql_num_rows($result)>0) {
// your thing above with mysql_fetch_array($result1) etc
} else {
echo 'nor found';
}

Categories