get url value multiple if statments to change output - php

I have a high-score.php page, depending on which link is pressed there is a value added to the url.
Example
/high-score.php?score=image_set_1
To get the value of the url I use the following:
<?php
$pack=$_GET['score'];
?>
Then I can echo the results
<?php echo $pack; ?>
This will output: image_set_1
What I would like to do is change image_set_1 to display Fruit and then the same for all the other Image_set
This is what I have so far
<?php
$pack=$_GET['score'];
if ($pack = "image_set_1"){
$pack = "Fruit cards";
}
else if ($pack = "image_set_2"){
$pack = "Number cards";
}
else if ($pack = "image_set_3"){
$pack = "Animal cards";
}
else if ($pack = "image_set_4"){
$pack = "Vegetable cards";
}
?>
The problem is the output is always fruit no matter what the url value is.
If I change the code from else if to just if then it displays just vegetable cards

The correct Comparison Operator is == not =
if ($pack = "image_set_1") {
// code
}
Needs to be:
if ($pack == "image_set_1") {
// ---^
// code
}

You need == to compare values, a single = sets a value.
if ($pack == "image_set_1"){
$pack = "Fruit cards";
}
Like so.
Reference: http://www.php.net/manual/en/language.operators.comparison.php

Related

How to number items in for-loop php

I have this code that select content from database and loop all, and few items are specify to be different by adding method (type-A and type-B) in database, so now i want to give all type-B numbers like 1,2,3 depending on number of type in database.
I could have do this using the id in database but it not number accordingly so i need just assign new number to list how many of it i have.
<?php
$getReply = $reply_stmt->getAll(); // this from my database
if(!is_null($getReply)){
foreach($getReply as $row){
$name = $row->itemname;
$method = $row->method;
}
if($method == 'type-B'){
$number = 1;
$number++;
echo $number."B-class<br/>".$name."".$method;
}else{
echo $name."".$method;
}
}
?>
Now i want to return something like this
1 B-class
Peter type-B
John type-A
2 B-class
Mike type-B
I am not sure but try to something like this...
$getReply = $reply_stmt->getAll(); // this from my database
if(!is_null($getReply)){
$number = 1;
foreach($getReply as $row){
$name = $row->itemname;
$method = $row->method;
if($method == 'type-B'){
echo $number." B-class<br/>".$name." ".$method;
$number++;
}else{
echo $name." ".$method;
}
}
}

Simplified anarchy "if statement"

Is there a way to code this function/executable..
if($img1 > "")
{
$show = $img1;
}
elseif($img2 > "")
{
$show = $img2;
}
elseif($img3 > "")
{
$show = $img3;
}
else
{
$show = 'default.jpg';
}
echo $show;
..in a more simple way? Thx.
Any time you end up using variables named 1,2,3,etc it means you should be using an array.
If you had it like this, for example:
$img = [
1=> '',
2=> '',
3=> 'image.jpg'
];
You could then process this like so:
// Remove empty values
$img = array_filter($img);
// Echo the first value found (image.jpg)
echo current($img);
Or to add your default:
echo (count($img)) ? current($img) : 'default.jpg';
This uses a ternary operator to echo 'default.jpg' if count($img) is 0.
If you know the id number of the image, you could do something like this:
<?php
$images = array(
'myimg.jpg',
'my_awesome_img.jpg',
'awesome_awesome.jpg'
);
$show = $images[$x];
?>
You can just do
if(!empty($img1)) {
echo $img1;
} elseif(!empty($img2)) {
echo $img2;
} elseif(!empty($img3)) {
echo $img3;
} else {
echo 'default.jpg';
}
given that you only ever want to display one of these.
Did not test, but something like this:
<?php
$img1 = 'hello';
$img2 = 'world';
$array = [1,2,3];
for($i=1; $i<count($array); $i++){
$v = ${"img{$i}"};
if( !empty($v) ){
$show = $v;
break;
}
}
echo $show;
No idea what you're trying to accomplish or why the code looks like that, seems poorly constructed from the get-go.

GET Multiple MySQL Rows, Form PHP Variables, and Put Into Json Encoded Array

I am trying to GET different rows from different columns in php/mysql, and pack them into an array. I am able to successfully GET a jason encoded array back IF all values in the GET string match. However, if there is no match, the code echos 'no match', and without the array. I know this is because of the way my code is formatted. What I would like help figuring out, is how to format my code so that it just displays "null" in the array for the match it couldn't find.
Here is my code:
include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
echo json_encode($fbaddra);
}
else
{
while($row = $res->fetch_array(MYSQLI_BOTH)) {
if($_GET['a'] == "fbaddra") {
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['addr'];
} else {
$fbaddr = null;
}
if ($row['facebookp'] === $_GET['facebookp']) {
$fbpaddr = $row['addr'];
} else {
$fbpaddr = null;
}
$fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
echo json_encode($fbaddra);
}
}
}
$mysqli->close();
UPDATE: The GET Request
I would like the GET request below to return the full array, with whatever value that didn't match as 'null' inside the array.
domain.com/api/core/engine.php?a=fbaddra&facebook=username&facebookp=pagename
The GET above currently returns null.
Requests that work:
domain.com/api/core/engine.php?a=fbaddra&facebook=username or domain.com/api/core/engine.php?a=fbaddra&facebookp=pagename
These requests return the full array with the values that match, or null for the values that don't.
TL;DR
I need assistance figuring out how to format code to give back the full array with a value of 'null' for no match found in a row.
rather than assigning as 'null' assign null. Your full code as follows :
include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
echo json_encode('no match');
}
else
{
while($row = $res->fetch_array(MYSQLI_BOTH)) {
if($_GET['a'] == "fbaddra") {
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fpaddr = null;
}
if ($row['facebookp'] === $_GET['facebookp']) {
$fbpaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fbpaddr = null;
}
$fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
echo json_encode($fbaddra);
}
}
}
$mysqli->close();
You can even leave else part altogether.
Check your code in this fragment you not use same names for variables:
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fpaddr = 'null';
}
$fbaddr not is same as $fpaddr, this assign wrong result to if statement.
It was the mysql query that was the problem.
For those who come across this, and need something similar, you'll need to format your query like this:
** MYSQL QUERY **
if ($_GET['PUTVALUEHERE']) {
$g = $_GET['PUTVALUEHERE'];
$gq = $mysqli->real_escape_string($g);
$q1 = "SELECT * FROM `addrbook` WHERE `facebookp` = '".$gq."' OR `facebook` = '".$gq."'";
}
** PHP CODE **
if($_GET['PUTVALUEHERE']{
echo json_encode($row['addr']);
}

If Post values equal other than other post values echo"failed";?

I have two combo boxes, one to report scores and one to set who scored the goals, How can i make it so if on post['submit']
If $_POST['Score1'] and $_POST['Score2']
is other than equal to
$_POST['homegoalscorer1'] and $_POST['awaygoalscorer1']
then echo"fail";
Something like;
if(isset($_POST['submit']))
{
$homescore = $_POST['Score1'];
$awayscore = $_POST['Score2'];
$homegoalscorer = $_POST['homegoalscorer1'];
$awaygoalscorer = $_POST['awaygoalscorer1'];
if '$homescore' + '$awayscore' != $homegoalscorer + $awaygoalscorer {
echo "failed";
}
else {
}
}
Any ideas?
Single quotes on a variable will turn that variable intro a string without execution. Also you forgot to add brackets:
if(isset($_POST['submit'])) {
$homescore = (float)$_POST['Score1'];
$awayscore = (float)$_POST['Score2'];
$homegoalscorer = (float)$_POST['homegoalscorer1'];
$awaygoalscorer = (float)$_POST['awaygoalscorer1'];
if (($homescore+$awayscore) != ($homegoalscorer+$awaygoalscorer)) {
echo "failed";
} else {
}
}
Use some brackets in your if statement to force the conditional setting in the correct context - and why are you encapsulating your variables in single quotes?
if (($homescore + $awayscore) != ($homegoalscorer + $awaygoalscorer))
{
// Your code continues....

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