Why can't I get the echo right? echo $tot gives me 3
For some reason the $row variable will have a value greater than 3 echo 'wrong' at if($row < $b)
<?php
if($row < $b) {
echo "good";
} else { echo "wrong"; }
$sql=mysql_query("SELECT ID FROM emp WHERE user='$login_session'"); // its currently zero
$row=mysql_num_rows($sql);
$b = 3;
$tot=$b - $row;
echo $tot;
?>
Use this:
<?php
$sql=mysql_query("SELECT ID FROM emp WHERE user='$login_session'"); // its currently zero
$row=mysql_num_rows($sql);
$b = 3;
$tot=$b - $row;
echo "$tot";
if($row < $b) {
echo "good";
} else { echo "wrong"; }
?>
You have been trying to output variables, which is not defined.
You code was:
Ouput undefined variables
Assign something to this variables
In my code order is:
Assign something to this variables
Ouput undefined variables
Related
I am displaying data from mysql using foreach but there is also a if condition inside the for each. I want to display a message if this condition is not matched. the code is below;
<?php
// some other codes to connect data base etc.
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
$lon[$i] = $row['lng'];
$lat[$i] = $row['lat'];
$status[$i] = $row['status'];
$title[$i] = $row['title'];
$property_id[$i] = $row['property_id'];
$price[$i] = $row['price'];
$availability[$i] = $row['availability'];
$type[$i] = $row['type'];
$bedrooms[$i] = $row['bedrooms'];
$lounges[$i] = $row['lounges'];
$type[$i] = $row['type'];
$time[$i] = $row['time'];
$i++;
}
for($i=0; $i<$count; $i++) {
$distance = Haversine($my_lat, $my_lon, $lat[$i], $lon[$i]);
if($distance < $radius) {
echo 'Display data';
} else{
echo "error message";
}
}
?>
After displaying message following this code:
if($distance < $radius) {
I want to display message if this condition is not met but nothing seem to work so far.
if($distance<$radius){
echo "This statement meets the criteria.";
}else{
echo "This statement doesn't meet the criteria.";
}
You should check the basics on how if/else works before diving into this.
Before make sure that your datatype is define.
http://php.net/manual/en/function.settype.php
Try not allow empty data:
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
After verify mojore points, simplify your conditions by:
elseif($distance >= $radius){
Like this you avoid Unexpect values.
first you print $distance and $radius and after put if condition so you can confirm what actually happened...
I have the following bit of code and for some reason in the first WHILE loop the first value of $actor_list (when $i = 0) does not display. If I simply echo $actor_list[0] then it displays fine, but in the loop it will not display. I merely get [td][/td] as the output. The remaining values of the array display fine.
Also the line
echo '</tr><tr> </br>';
is not displaying.
What am I missing here? The value of $num_actors is an even number in my test scenario so there doesn't seem to be a reason for the above echo line to be skipped.
$actor_list = explode(" ", $actors);
$num_actors = count($actor_list);
if ($num_actors <= 6) {
foreach ($actor_list as $actor) {
echo '[td]'.$actor.'[/td] </br>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
echo '</tr><tr> </br>';
while ($i <= $num_actors) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
}
}
You're not initialising the variable $i to 0, which means it will be set to 'null' and thus not reference the 0th index of the array; but when it's then incremented its value will become 1.
Note: [..] Decrementing NULL values has no effect too, but incrementing them results in 1.
See: http://php.net/manual/en/language.operators.increment.php
Try adding:
$i = 0;
before the if statement.
Made a few changes.
This is working for me.
Try it out.
<?php
$actor_list = 'act1 act2 act3 act4 act5 act6';
$actors = explode(" ", $actor_list);
$num_actors = count($actor_list);
//debug
//echo "<pre>";
//print_r($actor_list);
//echo "</pre>";
echo "<table>";
if ($num_actors <= 6) {
foreach ($actors as $actor) {
echo '<tr><td>'.$actor.'</td></tr>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
while ($i <= $num_actors) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
}
}
echo "</table>";
?>
I intend to grade the elements of a numerically indexed array in php using function call; my function call does not work: it doesn't grade the elements and I cannot figure out the error in the code.Please help
Thanks, in advance
<?php
//function intended to grade array elements
function gradeArray($x){
if($score>= 70){
echo"A";
}
elseif($score >= 50){
echo"B";
}
elseif($score>= 40){
echo"C";
}
else{
echo"F";
}
}
// Array of Scores to be graded
$scores = array ("55", "68", "43", "78");
//Display result in a tabular form
echo"<table border = '1'><th>Score</th><th>Grade</th>";
foreach($scores as $score){
echo"<tr><td>";
echo$score."</td>";
echo"<td>". gradeArray($score);
echo"</td></tr>";
}
echo"</table>";
?>
You are passing $x into your function then calling $score.
Your scores array is also in string format, just need to remove the quotes to make them numbers.
Also change $x to $score and it should work fine! :)
<?php
//function intended to grade array elements
function gradeArray($score) {
if ($score >= 70) return "A";
elseif ($score >= 50) return "B";
elseif ($score >= 40) return "C";
else return "F";
}
// Array of Scores to be graded
$scores = array (55, 68, 43, 78);
//Display result in a tabular form
echo "<table border='1'><th>Score</th><th>Grade</th>";
foreach ($scores as $score) {
echo "<tr><td>$score</td><td>" . gradeArray($score) . "</td></tr>";
}
echo "</table>";
?>
Your array elements are in string .convert all element to int using
gradeArray($x){
$score=(int)$x;
}
Try this it will work
First off, most likely $score is undefined in your function since you use $x.
function gradeArray($x){
Then you are using your if conditions as if($score>= 70){.
Also, in your return values, just use return.
return"A"; // and others
Use return not echo so that this concatenation echo "<td>". gradeArray($score); works.
$score is undefined in your variable, the function is fetching the variable as $x try to change $score to $x or define $score., you can use global $score also in your function. Also you should return the values instead of using echo use the code below
<?php
//function intended to grade array elements
function gradeArray($x){
$score=$x;
if($score>= 70){
return "A";
}
elseif($score >= 50){
return "B";
}
elseif($score>= 40){
return "C";
}
else{
return "F";
}
}
// Array of Scores to be graded
$scores = array ("55", "68", "43", "78");
//Display result in a tabular form
echo"<table border = '1'><th>Score</th><th>Grade</th>";
foreach($scores as $score){
echo"<tr><td>";
echo$score."</td>";
echo"<td>". gradeArray($score);
echo"</td></tr>";
}
echo"</table>";
?>
Hope this helps you
Try this
<?php
//function intended to grade array elements
function gradeArray($x) {
if ($x >= 70) {
return "A";
} elseif ($x >= 50) {
return "B";
} elseif ($x >= 40) {
return "C";
} else {
return "F";
}
}
// Array of Scores to be graded
$scores = array("55", "68", "43", "78");
//Display result in a tabular form
echo"<table border = '1'><th>Score</th><th>Grade</th>";
foreach ($scores as $score) {
echo"<tr><td>";
echo$score . "</td>";
echo"<td>" . gradeArray($score);
echo"</td></tr>";
}
echo"</table>";
?>
function grading( $marks ){
$grade = mysql_query("SELECT grade_name,grade_point FROM table_name **strong text**WHERE smark <= round($marks) AND hmark >= round($marks)");
$gd = mysql_fetch_row( $grade );
return $gd[0];
}
Got this code which is troubling me and am eager to accept any help!
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
break;
}
else
{
$x++;
}
}
When I include the break; it returns :
You have selected the 1
However, when I remove the break; it returns
You have selected the 1 You have selected the 1 You have selected the 1 You have selected the 1
There are currently 6 records in the database and if the code were to work it would display "You are 4th"
Any ideas?
Your $x++; only executes on else. In order to have it increment on every iteration you need to remove the else:
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
break;
}
$x++;
}
Just a side note: $row doesn't seem to be related to $me and $x here. I'll assume your loop contains some other code that you've omitted, but this alone will probably answer your question.
You are icrementing $x in the else, this is never getting executed.
You need to do the increment in the same part as the echo.
So either:
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
$x++;
}
else
{
$x++;
}
}
Or if you never need to increment $x without echoing:
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
x++;
}
}
I am trying to echo some text if my loop returns with no data but can't get it do work. I have tried a few things but no luck.
my code:
$result = mysql_send("SELECT * FROM datatable WHERE id='".
$_SESSION['id']."'ORDER BY id ASC LIMIT 2");
while($row=mysql_fetch_array($result)) {
$a = 1;
extract($row);
echo 'Trans ID: ';
echo $row['trans_id'];
echo '<br>';
echo 'Amount: ';
echo $row['amount'];
echo ' ';
echo $row['euros'];
echo '<br>';
echo '<br>';
}
if ($a = 1) {
echo 'No History';
} else {
echo 'View history';
};
Can Anyone help me with how to do if statements on a loop`?
You have an assignment, which returns the result of the assignment (i.e. 1)
if ($a = 1) {
instead of a comparison, which is what you probably want:
if ($a == 1) {
Therefore, "No history" will always be echoed.
use mysql_num_rows(); it will tell you if you have any results from query
$result = mysql_send("SELECT * FROM datatable WHERE id='".
$_SESSION['id']."'ORDER BY id ASC LIMIT 2");
$count = mysql_fetch_array($result);
if($count > 0) {
while($row=mysql_fetch_array($result)) {
# your code
}
}
# NOTE THE == not =
if ($a == 1) {
echo 'No History';
} else {
echo 'View history';
};
You will also have issue with your if statement as youu are using assigment rather than comparison: $a = 1 will set $a = 1 and $a == 1 will check of it equals 1
if($a == 1) ....
== is a comparison, = is an assignment.
Change to
if ($a == 1) {