Php flip the echo result - php

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

Related

If, elseif and else statements returning incorrect results

I am having some problem creating this fail catch in PHP. I want require 'error.php'; to be shown when a user enters a bad URL, which returns $echo_error_404 = 1.
I use normal PHP routing, which means, my URL gets split up into /example1/example2/example3/.
I have this page projects which is the same as $routes[1] when I enter that page. The file projects.php does not exist. Understood correctly projects should fall in under the second elseif statement. And then be given $error_echo_404 = 1 when file_exists() is used. However... for some weird reason it continues all the way into $echo_content = '<div id="content-wrap">'.$php_doc_html."</div>";
PS: Also I know a lot of my code is poorly formatted, however, I did it trying to solve my problem.
The code to check what files to require, and run error check:
// FIND WEBSITE CONTENT
$echo_content = "";
if(empty($routes[1])){
require 'frontpage.php';
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$front_page_html."</div>";
}
}
elseif((!empty($routes[1])) && ($routes[1] == "page")){
require 'frontpage.php';
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$front_page_html."</div>";
}
}
elseif((!empty($routes[1])) && ($routes[1] != "page")){
$php_doc = $routes[1];
$file_exist = $php_doc.".php";
if(file_exists($file_exist)){
require $php_doc.".php";
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$php_doc_html."</div>";
}
if(empty($echo_content)){
$echo_error_404 = 1;
}
}
else{
$echo_error_404 = 1;
}
}
else{
$echo_error_404 = 1;
This is how I run the correct version if $echo_error_404 = 1:
if($echo_error_404 == 1){
require 'error.php';
$error_index_head = <<< EOF
HTML TAG OPENING, TITLE, HEAD AND BODY OPENING ETC.
EOF;
echo $error_index_head;
echo $header_html;
echo '<div id="content-wrap">'.$error_page_html.'</div>';
echo $echo_index_after;
echo $footer_html;
}
else{
echo $echo_index_head;
echo $header_html;
echo $echo_content;
echo $echo_index_after;
echo $footer_html;
}
This is what I get in the browser as return, clearly showing that $echo_error_404 was not assigned the value 1:
var_dump() results:
elseif((!empty($routes[1])) && ($routes[1] != "page")){
$php_doc = $routes[1];
var_dump($php_doc);
$php_doc_html = "";
var_dump($php_doc_html);
// THE CODE INBETWEEN
else{
$echo_error_404 = 1;
}
var_dump($php_doc_html);
var_dump($echo_error_404);
Return shows that projects is in $routes[1] and that $echo_error_404 is = 1:
string(8) "projects"
string(0) ""
string(0) ""
int(1)
Might be a bit cleaner to use switch(). If we first check that $routes[1] is empty or not, we should not need to do it again in later conditions. It does not make sense to me that you then check it agian in your elseif. If it was empty, it would satisfy the first if and not move on to the next parts of the statement.
// Assume no content
$echo_content = "";
// Check for content
if(empty($routes[1])){
require 'frontpage.php';
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$front_page_html."</div>";
}
} else {
switch($routes[1]){
case "page":
require 'frontpage.php';
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$front_page_html."</div>";
}
break;
default:
$file_exist = "{$routes[1]}.php";
if(file_exists($file_exist)){
require $file_exist;
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$php_doc_html."</div>";
}
} else {
$echo_error_404 = 1;
}
}
}
if(empty($echo_content) || $echo_content == ""){
$echo_error_404 = 1;
}

PHP verification data error

I have an problem with my PHP verification data.
This my code so far
$cek_saldo=mysql_query
("SELECT * FROM t_balance");
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
}
if ($b_id == '$badge_id' AND $mon == '$date_month' AND $b_type == '$jns_saldo')
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
else
{
if($_POST)
{
$query = "INSERT INTO t_balance(badge_id, balance_amount, month, balance_type, date_transaction)
VALUES ('$badge_id', '$saldo', '$bulan', '$jns_saldo', '$date_transaction')
";
$hasil = mysql_query($query);
if($hasil)
{
echo "<div class='emp_err success'>Balance transaction successfully added.</div>";
}
else
{
echo "<div class='emp'>Gagal menambahkan saldo.</div>";
}
}
}
The rule is :
Tabungan Wajib can be submit for 1 time per month. So if twice, it will give error : "Balance for this month has been added before."
Tabungan Tambahan can be submit more than 1 time per month. So if submit more than 1 time, it will saved.
Anyone have a suggestions ?
if ($b_id == "$badge_id" AND $mon == "$date_month" AND $b_type == "$jns_saldo")
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
Your code failed due to the single quotes in the comparison using variables try to learn the difference between single and double quoted strings in php
You don't need to quote.I suppose $badge_id $date_month $jns_saldo come from your form with $_POST
if ($b_id == $badge_id AND $mon == $date_month AND $b_type == $jns_saldo)
This validation will no work
reason being you are selecting multiple rows from database and assigning only the last on e to $b_id .. $mon etc
and you are using single quotes for comparison
RESOLUTION
select data based on some id if you can
$cek_saldo=mysql_query("SELECT * FROM t_balance where id = 'someid'");
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
}
set the error flag
$error = false;
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
if($b_id == $badge_id AND $mon == $date_month AND $b_type == $jns_saldo)
{
$error = true;
break
}
}
if($error)
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
Hope it helps
Use this.
$cek_saldo= mysql_query("SELECT * FROM t_balance");
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
if ($b_id == "$badge_id" && $mon == "$date_month" && $b_type == "$jns_saldo")
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
else
{
if($_POST)
{
$query = "INSERT INTO t_balance(badge_id, balance_amount, month, balance_type, date_transaction)
VALUES ('".$badge_id."', '".$saldo."', '".$bulan."', '".$jns_saldo."', '".$date_transaction."')";
$hasil = mysql_query($query);
if($hasil)
{
echo "<div class='emp_err success'>Balance transaction successfully added.</div>";
}
else
{
echo "<div class='emp'>Gagal menambahkan saldo.</div>";
}
}
}
}

If Its empty, return something in PHP

i have a code, it's getting data but when it gets nothing i want it to return something.
$upcoming = simplexml_load_file('http://api.website.com');
foreach($upcoming->trailer as $x => $updates) {
$content.= '<center><br><span> ' . $updates->embed . '</span></center>';
}
This is the code. It's getting data but when it got nothing, i would like it to say NO Videos.
How can i do that? I tried strlen() but i couldn't applied it.
I tried strpos but it didn't work either.
You can simply check the result of simplexml_load_file and act accordingly
if (false === $upcoming) {
echo "No Videos";
} else {
//your foreach loop here
}
$upcoming = simplexml_load_file('http://api.website.com');
if($upcoming == null || $upcoming == false){
$content = "No videos";
}
else{
foreach($upcoming->trailer as $x => $updates) {
$content.= '<center><br><span> ' . $updates->embed . '</span></center>';
}
}
Add some flag in your foreach to put a flag if anything has been printed or just check the $upcoming.
Then just check the flag and echo "no videos" your message.
Do it like this:
$Something=false;
$upcoming = simplexml_load_file('http://api.website.com');
foreach($upcoming->trailer as $x => $updates) {
if($updates->embed){
$Something=true;
}
$content.= '<center><br><span> ' . $updates->embed . '</span></center>';
}
if(!$Something){
echo "<center>No videos</center>";
}
Or:
$upcoming = simplexml_load_file('http://api.website.com');
if(!$upcoming){
echo "<center>No videos</center>";
}
else{
foreach($upcoming->trailer as $x => $updates) {
if($updates->embed){
$Soemthing=true;
}
$content.= '<center><br><span> ' . $updates->embed . '</span></center>';
}
}

IF Statement Inside Function

I am writing a if statement that if the two dates listed are equal it echo's the first date and if the they aren't(else statement) it echoes "date1-date2" I have it inside a function and I do not know if I am alowed to do that.Here is the error I am getting
Parse error: syntax error, unexpected T_IF
Here is my function:
function getfevent ($conn) {
$sql = "SELECT `name` FROM `event` WHERE `featured` = 0 LIMIT 0, 30 ";
$statement=$conn->prepare($sql);
$statement->execute();
while($row=$statement->fetch()) {
$eventname = $row['name'];
$row_id=geteventid ($conn,$eventname);
$row_end=geteventend ($conn,$eventname);
$date=if ($row_id == $row_end){
echo $row_id;
}
else {
echo $row_id " - " $row_end;
}
?>
<?php echo "<td>$eventname</td><td>$date</td></tr>"; ?>
<?php
}
}
getfevent($conn);
Probably you want this.
$date = $row_id == $row_end ? $row_id : $row_id . " - " . $row_end;
echo $date;
Don't forget. Use . to concat.
It's the same of
if ($row_id == $row_end) {
$date = $row_id;
} else {
$date = $row_id . " - " . $row_end;
}
Yes, of course If statements are allowed inside functions. Your problem is that you are setting your variable $date's value to the value of a T_IF, which is illegal.
Rather, you could do something like this:
$date=($row_id == $row_end);
if($date)
{
echo $row_id;
}
else {
echo $row_id." - ".$row_end;
}
Instead of:
$date=if ($row_id == $row_end){
echo $row_id;
}
else {
echo $row_id " - " $row_end;
}
Try:
$date;
if ($row_id == $row_end)
$date=$row_id;
else
$date=$row_id." - ".$row_end;
What you're doing in the original code is essentially telling PHP to set the value of variable $date to an if statement, which is, well, not possible.
The alternate piece of code creates a new variable $date, and then sets its value to $row_id or $row_id." - ".$row_end; depending on how the if statement evaluates.
The echo statement is used to print something to the output buffer.
you have to do this instead:
if ($row_id == $row_end) {
$date = something
} else {
$date = somethingElse
}
//do whatever you want with $date
try
$date=$row_id;
if ($row_id != $row_end)
{
$date.=" - ".$row_end;
}
which should be much shorter i gues

if else simple beginner issue

Good day guys,
I've made a sweet favorites function with php mysql and ajax, and its working great. Now I want to show 'favorite' when favorite = 0 and show 'unfavorite' when favorite = 1
if ($favorites == 0) {
$favorite = 'Favorite';
}
if ($favorites == 1) {
$unfavorite = 'unFavorite';
}
and echo it in the row as :
<div id="favorites">' .($favorite). ' ' .($unfavorite). '</div>
The problem is: when favorite = 0, both $favorite and $unfavorite are being shown. When favorite = 1 only $unfavorite is being shown correctly. Of course it should be $favorite OR $unfavorite. I assume the problem is clear and simple to you, please assist :)
Thanks in advance
It's easier to use just one variable:
$text = ''
if ($favorites == 0) {
$text = 'Favorite';
} else {
$text = 'unFavorite';
}
...
echo $text;
If you want to check $favorite, you are using the wrong variable in your control statement. Also, it is better coding practice to use elseif rather than if for that second if. One more thing: it's easier to manage one resulting variable.
$output = "";
if ($favorite == 0) {
$output = 'Favorite';
}
elseif ($favorite == 1) {
$output = 'unFavorite';
}
...
echo $output; // Or whatever you want to do with your output
Is $favorites an integer?
Anyway try using three equal signs (===) or else instead of the second if:
if ( $favorites === 0 )
{
// ...
}
else // or if ($favorites === 1)
{
// ...
}
You're making a toggle, so you only need one variable:
if(empty($favourites)){
$fav_toggle = 'Favorite';
} else {
$fav_toggle = 'unFavorite';
}
echo $fav_toggle;
Same code is working on me if I assigned $favorites = 0; or $favorites = 1;
You can also use if else
$favorites = 1;
if ($favorites == 0) {
$favorite = 'Favorite';
}
else if ($favorites == 1) {
$unfavorite = 'unFavorite';
}

Categories