if then nested statements cause page not to load - php

As per the suggestions of others I have added to the initial code. Same result:
I am having an issue with the following if statement.
When it finds results on either of the two conditions it works great and returns the results using the for each statement. If there are no results then the page doesn't load completely. I have tried using die; in a number of locations which did not improve the functions or make this work.
NEW CODE
if (!empty($query)) {
foreach ($query as $available) {
echo $available->column1 . "<br>\n";
}
} elseif (empty($query)) {
$query_plus->get_results($wpdb->prepare("SELECT column1 FROM mytable"));
foreach ($query as $available_plus_2) {
echo $available_plus_2->column1 . "<br>\n";
}
} elseif (!empty($query_plus)) {
echo "nothing here";
}

if (!empty($query)) {
foreach ($query as $available) {
echo $available->column1 . "<br>\n";
}
} elseif (empty($query)) {
$query_plus=$wpdb->get_results($wpdb->prepare("SELECT column1 FROM mytable"));
foreach ($query as $available_plus_2) {
echo $available_plus_2->column1 . "<br>\n";
}
} elseif (!empty($query_plus)) {
echo "nothing here";
}
The $wpdb-> is necessary before the prepare. Since WP is very bad at error reporting adding ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); to the top of the functions page will allow all fatal errors to be seen

Related

Php flip the echo result

I confused the method to flip the place of the echo result, below is my code
while ($looptools == 0) {
$mysqlihelper = " SELECT * FROM soal WHERE nomorsoal = $numberofmysqli ";
$mysqliquery = mysqli_query($konek, $mysqlihelper);
$resultquery = mysqli_fetch_assoc($mysqliquery);
$resulttextjudul = $resultquery['judul'];
if ($resulttextjudul == null) {
unset($resulttextjudul);
$resulttextjudul = "Tunggu Soal Berikutnya ! ..";
$nullerror = true;
} else {
}
if ($nullerror == true) {
echo "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">" . $resulttextjudul . "</div>";
} else {
echo "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">" . $resulttextjudul . "</div>";
}
if ($nullerror == true) {
mysqli_close($konek);
break;
} elseif ($looptools == 10) {
mysqli_close($konek);
break;
} else {
}
}
As you see in the "while", it's echo the first result and the second result below it, but I want the first result in below of the second result, can anyone tell me the method to do it?
I assume you mean you want to print all successes followed by all errors, or something like that. Here's how:
if($nullerror == true){
$errors .= "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>";
}else{
$successes .= "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>";
}
Then, when the while loop is done:
echo $successes ;
echo $errors ;
If you really want to work your way backwards through the results in $mysqliquery, that's a different answer.
UPDATE: to reverse the order of successes / errors on display, just put the latest additions in front:
if($nullerror == true){
$errors = "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>" . $errors;
}else{
$successes = "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>" . $successes ;
}
sorry if i answer my own question because i found it out myself
use mysqli_num_rows or often called mysqli count
http://php.net/manual/en/mysqli-result.num-rows.php

Flow control using if statements

I have 300+ rows of data that i am getting from various users of our cloud application. Users are using the web application on mobile and on computers.Since we are using responsive designs,it interests us we know what the users are using to access the application.
We are interested in the common known manufacturers like so
<?php
$dbh = new PDO('mysql:host=localhost;dbname=qs', 'qs_root', 'lepasswort');
$fc = $dbh->prepare("select str from test_str");
$fc->execute();
$i = 0;
while($result = $fc->fetch(PDO::FETCH_ASSOC)){
$one = $result['str'];
$cmd = "/usr/bin/perl str_processor.pl '$one' ";
$output = shell_exec($cmd);
$arr = explode( ',', $output);
$the_type = $arr[0];
if($the_type == 'samsung'){
echo 'samsung';
}
if($the_type == 'nokia'){
echo 'samsung';
}
if($the_type == 'iphone'){
echo 'samsung';
}
if($the_type == 'ipad'){
echo 'samsung';
}
else{
echo 'exception';
}
}
?>
The known brands are samsung,nokia,iphone etc.
However,there are many brands that are not well known and we want to ignore them.
My script looks like i have written above,only that i have left the actual code between my if's.
When i run the script,i get exception meaning none of the code inside ifs is getting executed. Is if the right method here?.
Note:
I have checked and counter-checked my pl script and its correct so it cannot be it.
You must use elseif otherwise you will obtain "exception" everytime the terminal is not an ipad. All the conditions are mutually exclusive.
if ($the_type == 'samsung') {
echo 'samsung';
} elseif ($the_type == 'nokia') {
echo 'nokia';
} elseif ($the_type == 'iphone') {
echo 'iphone';
} elseif ($the_type == 'ipad') {
echo 'ipad';
} else {
echo 'exception';
}
An other way to do it, is to use an array:
$models = ['samsung', 'nokia', 'iphone', 'ipad'];
if (in_array($the_type, $models))
echo $the_type;
else
echo 'exception';
If you don't get any result except "exception", check $the_type with var_dump to be sure it contains what you hope.

Code causing page to render blank with PHP

So this is the first PHP script (if it's even called that?) that I've ever written from scratch, and I'm having an issue in that when it's applied to the existing (and otherwise working) page, the page shows up blank. I was hoping one of the many people who are better and more experienced than I am can take a look and find what is no doubt a blatant syntax error. Thank you in advance to anyone that shows me the light!
<?php
$sql = "SELECT * FROM 'jos_downloads_files'";
$rows = $db->fetch_all_array($sql);
foreach($rows as $row) {
$filename = $row['filetitle'];
$filepath = $row['realname'];
$featured = $row['featured'];
$id = $row['containerid'];
}
foreach ($id as $containername) {
if ($id == 2) {
$containername ="Incidental Information";
}
if ($id == 3) {
$containername ="Monitoring Reports";
}
if ($id == 4) {
$containername ="Agendas";
}
if ($id == 5) {
$containername ="Decision Prep";
}
if ($id == 6) {
$containername ="Agendas";
}
if ($id == 7) {
$containername ="Policy Governance";
}
echo '<div class = "moduletable">
<h3>' . $containername . '</h3>';
foreach ($featured as $featureedtrue) {
if ($featuredtrue == 1) {
echo '<ul class="weblinks">
<li>
<a>' . $filename . '</a>
</li>';
}
}
}
?>
As celeriko mentioned, you never declared $db prior to using it. As Xatenev mentioned, I'm not sure what fetch_all_array is, perhaps fetchAll()? Finally, as Valery Statichny mentioned, $id will never be an array.
In the future, it is helpful to turn on error reporting so that you can see where your scripts are crashing. You can do so by adding the following:
ini_set('display_errors',1);
error_reporting(E_ALL);
In production environments, turn error reporting off so that users don't see error messages.
Edit
More from Xatenev:
Why are you looping foreach $featured? $featured can only be ONE entry at the moment. You have to write $featured[] = $row['featured']; to get an array where u can loop through. Same to $id. 5. You are looping through $featured but use $filename then? $filename will print an array EVERY TIME you loop through $featured. So for example when you have 100 entries in $featured, you have 100x the same content when you print out $filename. – Xatenev

php while loop running only once

I have a php script for check the availability of some data. I call this script from external jquery. the jquery is running fine. here is my php:
<?php
$avares = checkAva($fi_nm, $tbl_nm, $txtval);
echo $avares;
function checkAva($field, $table, $curval) {
$avres = mysql_query("SELECT " . $field . " FROM " . $table . "") or die("query failed");
while ($a_row = mysql_fetch_array($avres)) {
$dbval = $a_row[$field];
if ($curval == $dbval) {
return "no";
} else {
return "yes";
}
}
}
?>
$curval is the variable coming from external jquery. my problem is that the while loop seems to run only once though there are lot of entries in the DB. I checked it with an integer variable and the while loop seems to run only once. can you help me to solve that?
Look at your code.
while ($a_row = mysql_fetch_array($avres)) {
$dbval = $a_row[$field];
if ($curval == $dbval) {
return "no";
} else {
return "yes";
}
}
you have used return, if its true it returns and false then also returns change those according to your needs. The return statement immediately ends execution of the current function
It will by design as you have a return statement. From what you have said your not actually wanting it to return but to set a variable that at end of execution will return no or yes. I could be wrong on this but hey ho.
<?php
echo checkAva($fi_nm, $tbl_nm, $txtval);
function checkAva($field, $table, $curval) {
$avres = mysql_query("SELECT " . $field . " FROM " . $table) or die("query failed");
$noOrYes = "yes";
while ($a_row = mysql_fetch_array($avres)) {
if($curval == $a_row[$field]) {
$noOrYes = "no";
}
}
return $noOrYes;
}
?>
The possible issue that can cause Loop to iterate once are:
Error in the Variable used for the $query and $result
Same name Variable inside and outside of the Loop
Incorrect placement of Return statement
Invalid Mysql Statement
Directly put the condition in your Query like
function checkAva($field, $table, $curval) {
$avres = mysql_query("SELECT " . $field . " FROM " . $table . "
WHERE `".$field."` = '".$curVal."'");
$res = mysql_fetch_array($avres);
if(is_array($res) && count($res) > 0)
return "Yes";
else
return "No";
}
Instead of getting all the results and checking with each one of the result you directly put a condition to extract the results which satisfies your condition.This will be suggestable if you have many records.
You need to put one of the return outside of the while loop.
For example if you just wanted to check if $curval == $dbval
while ($a_row = mysql_fetch_array($avres)) {
$dbval = $a_row[$field];
//If the condition was met return with a no
if ($curval == $dbval) {
return "no";
}
}
//If the condition was not met return yes
return yes;
That's basically what you need to do so the loop will run until your condition was met or not at all.

php for loop: error message if condition not met

My foreach loop:
$empty = "You cannot decommission this truck. It is in use."
$msg = "";
foreach ($trucks_to_remove as $truck_id) {
$truck_id = trim($truck_id);
if (strlen($truck_id)) {
$msg .= decom_truck(
$db,
$depot_id,
$truck_id
);
}
else
{
echo $empty;
}
}
echo $msg;
If the condition is not met, it means the sql cursor that preceded this foreach loop did not return a result for this truck_id (meaning it is in use), I just want a simple message sent to the page to alert the user.
It does not have to be a pop-up or the like. Where would I put my write or print statement? I assume I'd use an else clause but when I add an else inside the foreach loop (see above), I get nothing printed to the page. I have also tried the following:
else if (strlen($truck_id)) == 0
{
echo $empty;
}
I am very new to php.
$arr1 = array();
if(!$arr1)
{
echo 'arr1 is emty<br/>';
$arr2 = array(1, 2);
if($arr2)
echo 'arr2 has data';
exit();
}

Categories