While or foreach loops with ifs - php

I would like your help because I'm stuck on here:
SQL:
$testEvents = $db->prepare("SELECT * FROM events WHERE str_to_date(concat(`eventDate`, ' ', `time`), '%d-%m-%Y %H:%i')");
$testEvents ->execute();
PHP:
<?php
while ( $test = $testEvents->fetch(PDO::FETCH_ASSOC) ) {
if( ! is_numeric($test['eventName'] && is_numeric($test['teamA']) && is_numeric($test['teamB']))) {
echo $test['eventName'];
echo "<br>";
}
}
?>
My target is try to do like:
Database image
Example: If eventName IS NOT a number and teamA & teamB ARE NOT NULL , show eventName data.
AND
If teamA & teamB ARE NOT a number and eventName IS a number, show teamA and teamB
EXPLANATION IN IMAGE:
Explanation

Well apart from your strange database design... to answer your question you can try this...
I made up some test data to simulate your database
$data = [
[
'eventName'=>'1',
'teamA'=>'Magallanes',
'teamB'=>'Caracas',
'eventDate'=>'17-11-2016',
'eventTime'=>'11:11'
],
[
'eventName'=>'Manana',
'teamA'=>'123',
'teamB'=>'123',
'eventDate'=>'17-11-2016',
'eventTime'=>'12:12'
]
];
Then tested out the code to perform your loop... You were on the right track but obviously you just gave up....
foreach($data as $test) {
if( ! is_numeric($test['eventName']) && is_numeric($test['teamA']) && is_numeric($test['teamB'])) {
echo $test['eventName'];
echo " ";
echo $test['eventDate'];
echo " ";
echo $test['eventTime'];
echo "<br>";
}
else if (is_numeric($test['eventName']) && ! is_numeric($test['teamA']) && ! is_numeric($test['teamB'])) {
echo $test['teamA'];
echo " x ";
echo $test['teamB'];
echo " ";
echo $test['eventDate'];
echo " ";
echo $test['eventTime'];
echo "<br>";
}
}
The Results
Magallanes x Caracas 17-11-2016 11:11
Manana 17-11-2016 12:12
Swap and change things as you require.

<?php
while ( $test = $testEvents->fetch(PDO::FETCH_ASSOC) )
{
if( ! is_numeric($test['eventName']) && is_numeric($test['teamA']) && is_numeric($test['teamB']))
{
echo $test['eventName']." ".$test['eventDate']." ".$test['eventTime']."<br>";
}
else if (is_numeric($test['eventName']) && ! is_numeric($test['teamA']) && ! is_numeric($test['teamB']))
{
echo $test['teamA']." x ".$test['teamB']." ".$test['eventDate']." ".$test['eventTime']."<br>";
}
}
?>

Related

If meta key's value is x then echo

I have 3 pieces of code that essentially should do the same thing, but none of them are working and they are all echoing "Not Working!".
The meta key is wilcity_belongs_to and the value is 2980. These are all based on what plan a listing is on. So if the listing plan is value 2980 then it must echo "Working!".
$meta = get_post_meta( get_the_ID(), 'wilcity_belongs_to',true );
if( $meta == '2980' ) {
echo "Working!";
} else {
echo "Not Working!";
};
and
global $wpdb;
$wpdb->get_results( "select * from $wpdb->wpam_postmeta where meta_key = 'wilcity_belongs_to' " );
if ( $wilcity_belongs_to == '2980') {
echo "Working!";
} else {
echo "Not Working!";
};
and
if ( get_post_meta( $post->ID, 'wilcity_belongs_to', true ) == '2980' ) {
echo "Working!";
} else {
echo "Not Working!";
};
Here is a screenshot of what is in the database: Database
This is in Table: wpam_postmeta
Please could i get help on this, ive been trying everything.
Did you try this way? get_results() usually returns an array with the result set.
global $wpdb;
$result = $wpdb->get_results( "select * from $wpdb->wpam_postmeta where meta_key = 'wilcity_belongs_to' " );
foreach ($result as $row) {
if ( $row["wilcity_belongs_to"] == '2980') {
echo "Working!";
} else {
echo "Not Working!";
};
};

How do I change php if statement based on a variable without repeating code?

Let's say i have this:
if( $row['date'] == "$date" && $row['datestatus'] !== "NOT STARTED" ) {
//big chunck of code
}
That means show any status except for not started. Now I want to show all statuses if a variable is 1. Let's say I have a variable like this:
$var = 0;
That variable can be a 1 or 0 depending on user interaction. If it's a 0, work like that above if statement. If it's a 1, show records with all statuses. Basically like this:
if( $row['date'] == "$date" ) {
//big chunck of code
}
What I don't want to do is something like this:
if ($var==0) {
if( $row['date'] == "$date" && $row['datestatus'] !== "NOT STARTED" ) {
//big chunck of code
}
} else {
if( $row['date'] == "$date" ) {
//same big chunck of code
}
}
I tried doing something like this, but it didn't work:
if ($var==0) {
$phpextended = '&& $row["datestatus"] !== "NOT STARTED"';
} else {
$phpextended = '';
}
if( $row['date'] == "$date" $phpextended ) {
//big chunck of code
}
Possible duplicate Q&A didn't help. What helped was the answers on this page.
You need and OR statement:
if( $row['date'] === $date
&&
($row['datestatus'] !== "NOT STARTED" || $var === 1)
) {
//big chunck of code
}
if($var==0 && $row['date'] == "$date" && $row['datestatus'] !== "NOT STARTED" ||
$var==1 && $row['date'] == "$date"){
//big chunck of code
}

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

PHP: Echoing HTML When Multiple Checkboxes Options Are Checked

I've edited this question, because I felt I needed to be clearer in my request.
The code below relates to options checked in a checkbox. It will echo 'both values' when they are both checked, and it will echo 'only valueA', I cannot get it to echo 'only valueB' which instead prints blankly with no echo. Thoughts?
<?php
if (( $a == "General" ) && ( $b == "Specialist" )) {
echo '<h2>both values are printed in HTML</h2>';
}
?>
<?php
if (( $a == "General" ) && ( $b != "Specialist" )){
echo '<h2>only valueA is printed in HTML</h2>';
}
?>
<?php
if (( $a != "General" ) && ( $b == "Specialist" )) {
echo '<h2>only valueB is printed in HTML</h2>';
}
?>
If you want to check whether a value is in array you should use in_array function. Your code should look like this:
<?php
$CheckBox = $form_data['field'][1]
$a = ( "valueA");
$b = ( "valueB");
$isAChecked = in_array($a, $CheckBox[1]);
$isBChecked = in_array($b, $CheckBox[1]);
?>
<?php
if ( $isAChecked && $isBChecked ) {
echo '<h2>both values are printed in HTML</h2>';
} else if ( $isAChecked ) {
echo '<h2>only valueA is printed in HTML</h2>';
} else if ( $isBChecked ) {
echo '<h2>only valueB is printed in HTML</h2>';
} else {
echo '<h2>nothing printed</h2>'
}
?>

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

Categories