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) {
Related
Well, I have created a simple higher/lower game script. Its working fine, but I want to limit the user actions to 3. This means that the user will have to guess the number with 3 moves. On third action I will execute query and save the user to mysql. How I can limit the actions/moves?
<?php
session_start();
function Start_Again() {
$number = rand(1,100);
$_SESSION['higherlower'] = $number;
echo "Select a Number below.";
Display_Form();
}
function Display_Form() {
echo "<table>";
for ($num=1;$num < 101;$num++) {
if (!preg_match("/(.*?)0/", $num)) { echo "<td><a href=\"?number=".$num."\">".$num."</td>"; }
else { echo "<td><a href=\"?number=".$num."\">".$num."</td></tr><tr>"; }
}
echo "</table>";
}
if (isset($_GET['number'])) {
$User_Number = $_GET['number'];
$Actual_Number = $_SESSION['higherlower'];
$count = 0;
if ($User_Number < $Actual_Number) { echo "Higher"; $count + 1; Display_Form(); }
elseif ($User_Number > $Actual_Number) { echo "Lower"; $count + 1; Display_Form(); }
elseif ($User_Number == $Actual_Number) { echo "Bingo, Correct Guess!<br>"; Start_Again(); }
echo $count;
}elseif (!isset($_POST['higherlower'])) { Start_Again(); }
?>
When you initialize the game, simply store the amount of tries in your session.
$_SESSION['tries'] = 3;
Then, when the user picks a number, lower the tries and check if it's 0.
$_SESSION['tries']--;
if ($_SESSION['tries'] <= 0) {
die("Enough! You've been clicking numbers all afternoon.");
}
Full Implementation
<?php
/**
* Higher-lower game
*/
if (!isset($_SESSION)) {
session_start();
}
function Start_Again() {
$number = rand(1,100);
$_SESSION['higherlower'] = $number;
$_SESSION['tries'] = 3;
echo "Select a Number below.";
Display_Form();
}
function Display_Form() {
echo "<table>";
$chunks = array_chunc(range(1, 100), 10);
foreach ($chunks as $chunk) {
echo "<tr>";
foreach ($chunk as $num) {
echo "<td><a href=\"?number=".$num."\">".$num."</td>";
}
echo "</tr>";
}
echo "</table>";
}
if (isset($_GET['number'])) {
$User_Number = $_GET['number'];
$Actual_Number = $_SESSION['higherlower'];
if ($_SESSION['tries'] <= 0) {
die("Oops! You're bad at this!");
}
if ($User_Number < $Actual_Number) { echo "Higher"; $count + 1; Display_Form(); }
elseif ($User_Number > $Actual_Number) { echo "Lower"; $count + 1; Display_Form(); }
elseif ($User_Number == $Actual_Number) { echo "Bingo, Correct Guess!<br>"; Start_Again(); }
$_SESSION['tries']--;
echo $_SESSION['tries'] . 'chances left';
} elseif (!isset($_POST['higherlower'])) {
Start_Again();
}
You are already starting a session so you can store the value in a session variable i.e. $_SESSION['count'];
session_start();
if( !isset( $_SESSION['count] ) ) $_SESSION['count'] = 1;
Later on in the code you will need to update the counter $_SESSION['count']++;
And check whether the person has used up all the guesses
if( $_SESSION['count'] > 3 ) { ..... }
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
when i test this code , it was not echo 8 > 3 (it's will echo 1 = 1)?
i check my code are correct but why not echo real result?
<?PHP
$number = 8;
if ($number = '0')
{
echo $number." = 0";
}
elseif ($number = '1')
{
echo $number." = 1";
}
elseif ($number = '2')
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
Comparison in PHP is done via the == operator, not the = operator.
<?PHP
$number = 8;
if ($number == '0')
{
echo $number." = 0";
}
elseif ($number == '1')
{
echo $number." = 1";
}
elseif ($number == '2')
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
Try this:
<?PHP
$number = 8;
if ($number == 0)
{
echo $number." = 0";
}
elseif ($number == 1)
{
echo $number." = 1";
}
elseif ($number == 2)
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
You were using assigning operator, instead of equal to operator, and secondly check with integer value instead of string.
The = operator assigns the value on the right hand side to the variable on the left.
if ($number = '0')
This sets $number to 0, which is a falsy value, so returns false.
elseif ($number = '1')
This sets $number to 1 which is a truthy value, which returns true. This is why your code outputs 1 = 1.
You need to instead use the == equality operator:
if ($number == '0')
...
elseif ($number == '1')
...
...
You are assigning a value to the variable $number, you'll need to use the == or better === to match the number against the value of the variable.
In this case you know $number is going to be an integer, it is best practice in PHP to compare as strict as possible, which is the === operator. You can use the == operator, however the variable will only be loosely matched at that point.
For example a boolean false will also match an if-statement containing if ($number == 0), which is not what you want.
Please check the documentation of PHP to see the full extend of comparison.
Such as the following:
<?PHP
$number = 8;
// See the triple equal sign here
if ($number === 0)
{
echo $number." = 0";
}
// and here
elseif ($number === 1)
{
echo $number." = 1";
}
// and here
elseif ($number === 2)
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
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
In this elementary code, I check if a record exists with a particular itemId for a particular user and country.
The record returns 1 inside the function, but when it's outside, it's something totally different.
function recordExists(&$d) {
global $conn, $userId, $userCountryCode;
$sqlFindRecord = "select count(itemId) as recordCount from onr_items_mod where itemTempId = :itemTempId and itemStatus IN(0,5,8) and itemPublishedStatus = 2 and itemUserId = :userId and itemUserCountryCode = :userCountryCode";
$countRecord = $conn->prepare($sqlFindRecord);
$countRecord->execute(array(
":itemTempId" => $d,
":userId" => $userId,
":userCountryCode" => $userCountryCode
));
$countRecord->bindColumn('recordCount',$d);
echo 'This is D '.$d.' ---';
$row = $countRecord->fetch();
if($d == 0) {
return $d;
} elseif ($d == 1) {
return $d;
} else {
return false;
}
}
$d = 'US01'
if(recordExists($d) == 0) { //If the function returned 0
echo $d;
echo 'Was Zero';
} elseif (recordExists($d) == 1) { //If the function returned 1
echo $d;
echo 'Was One';
} else {
echo $d;
echo 'Neither Zero or One';
}
After I do the above, it's always 'Neither Zero or One'. Why is this? I tried the entire morning, but just cannot figure a way to sort this out.
This works, but not the above
function counter(&$a) {
if($a == 0) {
return $a;
} elseif ($a == 1) {
return $a;
} else {
return false;
}
}
$a = 1;
if(counter($a) == 0) {
echo $a;
} elseIf (counter($a) == 1) {
echo $a;
} else {
echo 'Umm...'.$a;
}
It will always be 'Neither Zero or One' why? Let's seee how it works
1. $d = 'US01'
2. if(recordExists($d) == 0) { //If the function returned 0
3. echo $d;
4. echo 'Was Zero';
5. } elseif (recordExists($d) == 1) { //If the function returned 1
6. echo $d;
7. echo 'Was One';
8. } else {
9. echo $d;
10. echo 'Neither Zero or One';
11. }
First $d is equal US01. It's ok. But after pass line #2 $d == 1 because you have set it in function (you have pass $d by reference).
So if statement in #2 line will return false beacuse thera are some records in database with itemTempId = 'US01'.
But now in the second if statement in line #5 variable $d = 1. So I suposse that you don't have any record with that itemTempId in your database. So the second statement (line #5) will also return false.
In my opinion you shouldn't pass it by reference.
EDIT
If you want to check if item exist in databse then yopu can ask only once. I really don't know what your goal is. But maybe this will be good
$d = 'US01';
$ret = recordExists($d);
if ($ret === 0) {
echo 'Was zero';
} elseif ($ret === 1) {
echo 'Was one';
} else {
echo 'Something else';
}
try to insert that into session variable and retrieve it in another method.
Hope it'll help.
Trying to create DIV containers out of a mysql results which each hold chunks of 10.
$balloon_count = the amount of records each div should hold.
$ui = the loop counter.
Functionality is simple, don't want to a template engine.
Trying to use the MODULUS operator to simplify the div cuts.
Doesn't work. Any direction is greatly appreciated.
Sample Code
$ui=1;
$balloon_holds = 10;
while($row = mysql_fetch_array($result))
{
if($ui==1||$ui%$balloon_holds != 0)
{
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"'; $this->ischecked($uid,$row[id]); echo "/>".$row['name'].'<br>'."\r\n";
if($ui==10||$ui%$balloon_holds != 0){
echo '</td></tr></table></div>';
}
$ui++;
}
Sample Expected "HTML" Output
<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>
<td style="font-size:small;vertical-align:text-top;">
Record1
Record2
Record3
Record4
Record5
Record6
Record7
Record8
Record9
Record10
</td></tr></table></div>
<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>
<td style="font-size:small;vertical-align:text-top;">
Record11
Record12
Record13
Record14
Record15
Record16
Record17
Record18
Record19
Record20
</td></tr></table></div>
If I understand the question correctly, you want the <div> opener on $ui = 1,11,21,31,... and the </div> closer on 10,20,30,40,...
If this is correct, your modulus operators should be changed as such:
if($ui%$balloon_holds == 1)
{
...
}
...
if($ui%$balloon_holds == 0)
{
...
}
if($ui==1||$ui%$balloon_holds != 0)
This will always be true except for when $ui is a multiple of 10, so you'd be outputting your header/footer blocks for ALL rows except 0, 10, 20, etc... and the one case where $ui is 1.
Most like likely this would be easier to understand:
$row_cnt = 0;
$max_rows = 10;
while($row = ...) {
if ($row_cnt == 0) {
// output header
}
// output row data
if ($row_cnt == 9) {
// output row footer
}
$row_cnt++;
$row_cnt %= $max_rows; // ($row_cnt resets to 0 when it reaches 10)
}
Here is my suggestion, it's also a little more readable / maintainable.
$ui=0;
$balloon_holds = 10;
while ($row = mysql_fetch_array($result))
{
if ($ui % $balloon_holds)
{
if ($ui >= 10)
{
echo '</td></tr></table></div>';
}
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"';
$this->ischecked($uid,$row[id]);
echo "/>".$row['name'].'<br>'."\r\n";
$ui++;
}
if ($ui > 0)
{
echo '</td></tr></table></div>';
}
$ui=0;
$balloon_holds = 10;
while($row = mysql_fetch_array($result))
{
$exit = 0;
if($ui==1||$ui%$balloon_holds != 0)
{
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"'; $this->ischecked($uid,$row[id]); echo "/>".$row['name'].'<br>'."\r\n";
$ui++;
if($ui%$balloon_holds == 0){
echo '</td></tr></table></div>';
$exit = 1;
}
}
if($exit == false){
echo '</td></tr></table></div>';
}