I have a code where it should check if the result equals to 8 it need to show something and if not it need to show something else and all of that happens inside of a while loop.
while ($row_fpages2 = mysql_fetch_array($result_fanpage2))
{
if ( $row_fpages2['client'] != NULL ) {
//GRAPHS
$sql = "SELECT likes, date FROM statistics_pages WHERE idnum = '".$idnum."' AND page_name = '".$row_fpages2['page_name']."' ORDER BY `statistics_pages`.`date` DESC LIMIT 8";
$result2 = mysql_query($sql) or die(mysql_error());
if ($result2) {
$data = array();
while ($row = mysql_fetch_assoc($result2)) {
$data[] = $row["likes"];
}
if ($result2 == 8) {
$c_data = count($data)-1;
$final = array();
for ($i = 0; $i < $c_data; $i++) {
$final[] = getZeroResult($data[$i], $data[$i+1]);
}
$data_string = join(",", $final);
$stats = '<img src="http://chart.apis.google.com/chart?chs=240x140&cht=ls&chd=t:0,0|'.$data_string.'&chg=20,20&chls=0.75,-1,-1|6,4,1&chm=o,FF9900,1,-1,7,-1|b,3399CC44,0,1,0"></img>';
} else {
$stats = '<img src="images/stats_un.jpg"></img>';
};
} else {
print('MySQL query failed with error: ' . mysql_error());
}
echo '...';
The problem is that the first output always showing the ( == 8) (even if it is not equals to 8) instead of the else output.
Then if i have 2 or more everything comes above the first one is correct but the first one is still showing the ( == 8).
Any help?
You do the following which is incorrect:
$result2 = mysql_query($sql) or die(mysql_error());
...
if ($result2 == 8) {
The return value of mysql_query is a resource not an int. What is that you are trying to do there ?
May be you would like to use
if(strlen($result2) == 8){
...
}
instead of
if($result2 == 8){
...
}
Hope that solves your problem.
Related
There are three "BC" in the result_category but I am only getting 1 result. This is for a personality quiz. Please Help. I also tried $query = "SELECT result FROM quiz_map where result_category = 'BC'"; but still, only 1 result is showing.
$result = mysqli_query($link, $query);
$cat_a = $cat_b = $cat_c = $cat_d = $cat_e = 0;
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
$cat = $row['category'];
if ($cat == "A") {
$cat_a += 1;
} elseif ($cat == "B") {
$cat_b += 1;
} elseif ($cat == "C") {
$cat_c += 1;
} elseif ($cat == "D") {
$cat_d += 1;
} elseif ($cat == "E") {
$cat_e += 1;
}
}
$array = array('A' => $cat_a, 'B' => $cat_b, 'C' => $cat_c, 'D' => $cat_d, 'E' => $cat_e);
$str = '';
foreach ($array as $i => $value) {
if ($value >= 6) {
$str = $i;
break;
} elseif ($value >= 2) {
$str .= $i;
}
}
$var = sort($array);
$query = "SELECT result FROM quiz_map where result_category = '$str' LIMIT 1";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
echo $row[0];
?>
There's many things wrong with your code!
As pointed by #IsThisJavascript and #Cashbee:
You are executing a query with a LIMIT 1 statement, it will only return one record.
As pointed by myself:
Doing echo $row[0] will have the same result, if you are only echoing the first value of the array you can't expect to have multiples can you?
As pointed by #IsThisJavascript:
You need to loop the results array, like so:
while($row = mysqli_fetch_array($result)){
echo $row['result_category'];
}
Consider switching the query from a '=' to a '%like%' statement, to maximize results if you want to get the values that partionaly cointain the string.
I have a condition in which it is using a variable to pull either a number through 0-17, the string "MAKEUP", or the variable will be empty. I would like it to output the text "WIN" if the variable is greater than the number 8 and "LOSS" if the variable is less than the number 9. I would also like it to out "MAKEUP" if the variable consist of the string MAKEUP, and to display nothing if the variable is empty. Seems pretty simple to me, but I'm having issues particularly with the empty part. Can anyone let me know what I am doing wrong here? Code below
<?php
$t1w8 = '';
$result = $t1w8;
if ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result < 9 && !empty($result)) {
echo 'LOSS';
} elseif ($result == 'MAKEUP') {
echo '-';
} else {
echo 'yooo';
}
?>
Make some changes in your conditions like this
<?php
//$result = "MAKEUP";
$result = 0;
if ($result === 'MAKEUP') {
echo '-';
}else if (is_numeric($result) && $result < 9 ) {
echo 'LOSS';
}else if (is_numeric($result) && $result >= 9 ) {
echo 'WON';
} else{
echo 'yooo';
}
?>
Live demo : https://eval.in/897120
try with this code
<?php
//$result = "MAKEUP";
$result = "";
//$result = "9";
//$result = "-";
if ($result == 'MAKEUP' && !empty($result) ) {
echo '-';
} elseif ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result <= 8 && !empty($result)) {
echo 'LOSS';
} else {
echo 'yooo';
}
?>
for demo :demo code here
You have explained that your number range is from 0-17.
You have also explained that you could get the word MAKEUP.
Based upon those constraints we could use something like this
$output = "";
// Do we have something?
if(strlen($result) > 0) {
if (strtolower($result) == "makeup") {
$output = "MAKEUP";
}
// assumes a single digit string
else if ($result < 9) {
$output = "LOSS";
} else if ($result <= 17) {
$output = "WIN";
}
}
echo $output;
The following code shows an error
"Undefined offset: 2" on lines 22, 28, and 29
$sql = "SELECT email FROM CommercialEmails WHERE dripid = 1 AND sent='a'";
if ($resultsd1 = mysqli_query($conn, $sql )) {
$affectedrows = mysqli_num_rows($resultsd1);
while ($row = mysqli_fetch_row($resultsd1)){
$results = $row[0];
global $results;
}
}
$broken = explode(' ', $results);
$hi = 0;
$hello = 0;
a:
/**Line 22 **/ if (substr($broken[$hi], -4) == "com," && $broken[$hi] == "qwert"){
$hey[$hi] = $broken[$hi];
$hello++;
}
If(substr($broken[$hi], -4) !== "com,"){ // line 28
$hey[$hi] = $broken[$hi]; //Line 29
}
$hi++;
if ($hi == $affectedrows){
if (!isset($hey)){
echo "There are no emails";
} else {
foreach( $hey as $key => $value){
echo $value;
}
echo $hey;
}
}else{
goto a;
}
I am not sure what you are trying to achieve, but to get your code just working try this way:
$sql = "SELECT email FROM CommercialEmails WHERE dripid = 1 AND sent='a'";
$results = array();
$hey = array();
if ($resultsd1 = mysqli_query($conn, $sql )) {
$affectedrows = mysqli_num_rows($resultsd1);
while ($row = mysqli_fetch_row($resultsd1)){
$results[] = $row;
If(substr($row[0], -4) !== "com,"){
$hey[] = $row[0];
}
}
}
if (count($hey)==0){
echo "There are no emails";
} else {
foreach( $hey as $value){
echo $value;
}
echo $hey;
}
As you can see I completely removed many of your variables you don't need them until you can explain your goals.
And I've removed your weird condition if:
if (substr($broken[$hi], -4) == "com," && $broken[$hi] == "qwert"){
$hey[$hi] = $broken[$hi];
$hello++;
}
Because there is no such value of $broken[$hi] that can be equal 'qwert' and to have 'com' as substring inside. So this codition is always false and we can delete it.
Try to explain your goals. Hope I can help you.
It means that the $broken array does not have 3 elements (ofset index = 2).
I can not see where that $hi var is being set to 2 but it is somewhere in code you have not shown I guess.
Instead of global $results; inside the while loop, before the while loop starts just declare $results as an empty array $results = array();
Then after the while loop ends do print_r($results); to see if it has the contents you expect.
Working on a link shortening script and I'm stumped. I figured the following code would function as needed however, I get an execution time out related to $str .= $charset[mt_rand(0, $count-1)];. I have scoured over the code several times, I can't find what I am doing wrong.
function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
$str = '';
$count = strlen($charset);
while ($length--) {
$str .= $charset[mt_rand(0, $count-1)];
}
return $str;
}
function shrinkURL($url) {
$is_unique = false;
$num = 4;
$random_string = randString($num);
$count = 0;
while (!$is_unique) {
$q1 = "SELECT id FROM linkShortner WHERE short = '".$random_string."' LIMIT 1";
$result = mysql_query($q1) or die(mysql_error());
if ($result === false) // if you don't get a result, then you're good
$is_unique = true;
else // if you DO get a result, keep trying
$count++;
if ($count >= 10) {
$num = (strlen($random_string) + 1);
$random_string = randString($num);
$count = 0;
}
$random_string = randString($num);
}
$shortURL = "https://domain.com/l/".$random_string;
$q2 = "INSERT INTO linkShortner (id, destination, short, shorURL, creationDate) VALUES (NULL, '".$url."', '".$random_string."', '".$shortURL."', '".$DateTime."')";
$r2 = mysql_query($q2) or die(mysql_error());
return $shortURL;
}
$shortURL = shrinkURL('http://domain.com');
echo $shortURL;
Any help would be greatly appreciated, think maybe I am just burnt out.
My guess is that at some point your function randString() will be called with the $length argument being 0.
This would make:
while ($length--) {
$str .= $charset[mt_rand(0, $count-1)];
}
get stuck, because the first iteration would be while (-1), which is true. And then while (-2), which is also true.. etc etc etc.
I would change your while ( $length-- ) to while ( $length-- >= 0 )
Changed if ($result === false) to if (!mysql_num_rows($result)) and all is well. if ($result === false) is for mysqli which is not being used here.
Basically the shrinkURL function scours the DB for a matching string before trying to insert a unique string/row, if ($result === false) would never = false because the value of $result is MySQL_query($q1) therefore producing an endless loop.
I can't remove an item from query result array in php + codeigniter.
This is my code
if($query->num_rows > 0)
{
$rows = $query->result();
foreach ($rows as $key => $row)
{
$i = 0;
$fornecedor = $row->fornecedor;
$marca = $row->marca;
$modelo = $row->modelo;
$versao = $row->versao;
$preco = $row->preco;
foreach ($rows as $row2)
{
$fornecedor2 = $row2->fornecedor;
$marca2 = $row2->marca;
$modelo2 = $row2->modelo;
$versao2 = $row2->versao;
$preco2 = $row2->preco;
if(($fornecedor == $fornecedor2) && ($marca == $marca2) && ($modelo == $modelo2) && ($versao == $versao2) && ($preco == $preco2))
{
$i++;
}
}
if($i > 3)
{
unset($row[$key]);
}
}
return $query;
}
I already checked some examples here in stackoverflow but i cant make this work.
I can't see the problem ty
so $row is a $rows[$key], maybe i don't understand something, but it's seems to me you have to write unset($rows[$key]);