So, i have the following switch case (i've only put 3 for this example, but there are more...).
if (isset($_GET['cat'])) {
$get_category = trim($_GET['cat']);
switch ($get_category) {
case is_numeric($get_category):
echo 'is_numeric';
// execute query
break;
case 'newest':
echo 'newest';
// execute query
break;
case '':
echo 'empty';
// execute query
break;
}
}
When my $_GET['cat'] is empty, i'm getting is_numeric and i'm supposed to get empty.
I've tried looking at the $_GET['cat] array, and i get this:
Array
(
[cat] =>
)
I've also checked the type and i get string:
echo gettype($get_category); // returns: string
I've also use an if statement to verify if it picks up is numeric but it doesn't, it picks up string
if (is_numeric($get_category)) {
echo 'numeric';
} else {
echo 'string';
}
// returns: string
So, why is it that my switch statement does not pick up the correct case?
I am attempting to use the switch function of PHP but I must be doing something wrong. The code below will echo the result of the $find_top_type in the console but doesnt seem to go any further. I know the query is good having tested that and verified it through the ECHO. It doesnt seem to go further into the PHP however. It does not show me either of the other ECHO requests.
What am I doing wrong?
$find_top_type = $wpdb->get_var($wpdb->prepare("SELECT column_1 FROM mytable WHERE id = $tid"));
echo $find_top_type;
$find_top_type = 2;
$find_top_type = 4;
$find_top_type = 6;
$find_top_type = 8;
switch($find_top_type){
case 2:
echo "retuning two test";
break;
case "4":
echo "retuning four test";
}
I think you should add default statement and end of switch case and your case 4 have not break statement.
I have a query fetching product ID from the database, then base on the ID #, I have a switch statement that gets the image for the product. All works fine and at the end of the switch I have a default case where it would just print out an image that says "no image available".
Here is part of the code for imagequery.php
<?php
if($image = mysqli_fetch_assoc($imagequery))
{
switch($image['product_code'])
{
case 0:
echo '<th><img src="Images/pencil.jpg">';
break;
case 1:
echo '<th><img src="Images/pen.jpg">';
break;
case 2:
echo '<th><img src="Images/marker.png">';
break;
case 3:
echo '<th><img src="Images/clipper.png">';
break;
case 4:
echo '<th><img src="Images/paper.png">';
break;
default:
echo '<th><img src="Images/noimage.jpg">';
break;
}
}
Edit1: After doing more recommended echo checks, turns out the $image['imagequery'] is going in ascending order and I'm not sure why. My code to get $imagequery is this.
$results = "SELECT product_code FROM products WHERE product_desc='Office Materials'";
$imagequery = mysqli_query($mysqli, $results);
include('imagequery.php');
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Whats is the best way of optimistic programming in the cases of multiple if else. Heard that usage of multiple if else decreases the programming performance.???
If you have to take decision on the basic single value in this case switch case is very economic in comparison with if, else. suppose we have a variable $data can have various type of value then in this case use of switch case is very economic
switch($data){
case 'username':
// do the thing with username;
break;
case 'anotherdata':
// do the thing with another data;
break;
// do the same
default:
//do default thong
}
if you want to use switch on different variable then you can also use
switch(true) {
case isset($_GET['search']):
include_once 'template/template.search.php';
break;
// do more
default:
//do your work
break;
}
Hope it will help you....
Well if you have several IF statements or ELSEIF statements... you can use a 'SWITCH' instead of IF and ELSEIF. see below.
if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
}elseif ($i == 3) {
echo "i equals 3";
}
below is the replacment with switch :
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
case 3:
echo "i equals 3";
break;
}
The advantage of the above example is that if any one statement is satisfied the others will not run. They will break and the code will not continue to check the other conditions.
In the above example if $i == 0 the first statement will print and the rest wont be executed.
function addAds($n) {
for ($i=0;$i<=$n;$i++) {
while($row=mysql_fetch_array(mysql_query("SELECT * FROM users"))) {
$aut[]=$row['name'];
}
$author=$aut[rand(0,mysql_num_rows(mysql_query("SELECT * FROM users")))];
$name="pavadinimas".rand(0,3600);
$rnd=rand(0,1);
if($rnd==0) {
$type="siulo";
} else {
$type="iesko";
}
$text="tekstas".md5("tekstas".rand(0,8000));
$time=time()-rand(3600,86400);
$catid=rand(1,9);
switch ($catid) {
case 1:
$subid=rand(1,8);
break;
case 2:
$subid=rand(9,16);
break;
case 3:
$subid=rand(17,24);
break;
case 4:
$subid=rand(25,32);
break;
case 5:
$subid=rand(33,41);
break;
case 6:
$subid=rand(42,49);
break;
case 7:
$subid=rand(50,56);
break;
case 8:
$subid=rand(57,64);
break;
case 9:
$subid=rand(65,70);
break;
}
mysql_query("INSERT INTO advert(author,name,type,text,time,catid,subid) VALUES('$author','$name','$type','$text','$time','$catid','$subid')") or die(mysql_error());
}
echo "$n adverts successfully added.";
}
The problem with this function, is that it never loads. As I noticed, my while loop causes it. If i comment it, everything is ok. It has to get random user from my db and set it to variable $author.
The problem is that the query is in the loop, so it gets run every time (so you start from the beginning every time). Just move the mysql_query() part to right before the while loop and store it in a variable:
$query = mysql_query("SELECT * FROM users");
while($row=mysql_fetch_array($query))
You can replace this mega switch with one line:
$subid = rand(($catid * 8) - 7, min($catid * 8, 70));
The condition of a while loop is executed and evaluated with each iteration. So mysql_query is called with every iteration and retunrs true.
Just execute your database query once and cache the result:
function addAds($n) {
$result = mysql_query("SELECT * FROM users");
$aut = array();
while ($row = mysql_fetch_array($result)) {
$aut[]=$row['name'];
}
$rowCount = count($aut);
for ($i=0; $i<=$n; $i++) {
$author=$aut[rand(0,$rowCount)];
// …
mysql_query("INSERT INTO advert(author,name,type,text,time,catid,subid) VALUES('$author','$name','$type','$text','$time','$catid','$subid')") or die(mysql_error());
}
echo "$n adverts successfully added.";
}
I also think the problem is your functions are way too big to understand(quickly). You should make them smaller and test them with a unit testing framework like phpunit.
It's a lot of time that I don't use PHP but I think that the assignment
$row=mysql_fetch_array(mysql_query("SELECT * FROM users"))
should always returns true, it executes the query again and again on every iteration..
You're starting a new query each time you run the loop.