Jump to content

Mysql switch statement doesnt work right...


Russia

Recommended Posts

Hey guys, I have a mysql switch statement that shows different things based on the row value.

 

Here it is...

 

<?php
switch ($rows['icon']) {
    case 1:
        $picture = '<img src=\"img/apple.gif\" title=\"apple\" alt=\"apple\" />';
        echo $picture;
        break;
        
    case 2:
        $picture = '<img src=\"img/banana.gif\" title=\"banana\" alt=\"banana\" />';
        echo $picture;
        break;
        
    case 3:
        $picture = '<img src=\"img/orange.gif\" title=\"orange\" alt=\"orange\" />';
        echo $picture;
        break;
        
    default:
        echo "$rows[icon] is something other than 1 2 or 3";
        break;
}  
?>

 

 

Here is how the other code looks like.

 

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');


mysql_connect("localhost", "", "")or die("cannot connect");
mysql_select_db("test")or die("cannot select DB");
$tbl_name="test_mysql";
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if (isset($_POST['Submit'])) {
    for($i=0;$i<$count;$i++){
        $month   = $_POST['month'];
        $date    = $_POST['date'];
        $message = $_POST['message'];
        $title   = $_POST['title'];
        $id      = $_POST['id'];
        $icon    = $_POST['icon'];         // ILYA
        $monthday= $month[$i]."<br>".$date[$i];
        $sql1="UPDATE $tbl_name 
                    SET monthday='$monthday', 
                        month='$month[$i]', 
                        date='$date[$i]', 
                        message='" . mysql_real_escape_string($message[$i]) . "', 
                        title='" . mysql_real_escape_string($title[$i]) . "',
                        icon='$icon[$i]'                        
                    WHERE id=".$id[$i];                        // ILYA

    if(!($result1 = mysql_query($sql1))){                   
        "<BR>Error UPDATING $tbl_name ";                              
        exit();                                             
        }                                                   
    }
}
$result=mysql_query($sql);
$count=mysql_num_rows($result);


?>

 

Dont mind the other UPDATE code its part of something else.

 

Anyways, why is the case automaticaly going to the last one... with the words: is something other than 1 2 or 3

 

 

Also, this is with multiple rows at once so I think that that may the problem...

Link to comment
Share on other sites

Okay I think I for part of it to work:

<?php

while($row = mysql_fetch_assoc($result))
{

switch ($result['icon']) {
    case 1:
        $picture = '<img src=\"img/apple.gif\" title=\"apple\" alt=\"apple\" />';
        echo $picture;
        break;
        
    case 2:
        $picture = '<img src=\"img/banana.gif\" title=\"banana\" alt=\"banana\" />';
        echo $picture;
        break;
        
    case 3:
        $picture = '<img src=\"img/orange.gif\" title=\"orange\" alt=\"orange\" />';
        echo $picture;
        break;
        
    default:
        echo "$row[icon] is something other than 1 2 or 3";
        break;
}   



}  
?>

 

The thing is it still says

 

1 is something other than 1 2 or 3

2 is something other than 1 2 or 3

3 is something other than 1 2 or 3

3 is something other than 1 2 or 3

2 is something other than 1 2 or 3

1 is something other than 1 2 or 3

 

I need it to show the image.

 

The thing is the values in the rows are 1, 2, 3, 3, 2, 1.

 

In that order from the first row to the last.

Link to comment
Share on other sites

Geat. Now how can I echo the values around the website. Not exactly in order, like on the bottom of the page I want to echo like row 2's image BY ITSELF!

 

and another part I want to echo like the image from row 5. How would I exactly do that?

 

Basiclly I dont want it all in order.

 

 

Should i use something like:

 

<php?
echo $picture['2']
?>

 

The thing in the brackets [] would be the row i want to echo from...

Is that at all possible?

Link to comment
Share on other sites

$picture is not an array.  It is assigned exactly one value in the switch statement and is overwritten each time through the loop.  You could do $picture[$id] to create an array of every row:

 

while($row = mysql_fetch_assoc($result))
{
$id = $row['id'];

switch ($row['icon']) {
    case 1:
        $picture[$id] = '<img src=\"img/apple.gif\" title=\"apple\" alt=\"apple\" />';
        echo $picture[$id];
        break;
        
    case 2:
        $picture[$id] = '<img src=\"img/banana.gif\" title=\"banana\" alt=\"banana\" />';
        echo $picture[$id];
        break;
        
    case 3:
        $picture[$id] = '<img src=\"img/orange.gif\" title=\"orange\" alt=\"orange\" />';
        echo $picture[$id];
        break;
        
    default:
        $picture[$id] = '';
        echo $row['icon'] . " is something other than 1 2 or 3";
        break;
}   

 

Then echo $picture[1] would echo the picture from the row with id = 1.

Link to comment
Share on other sites

Oh my god thank you ssssoooo much :)

 

Also, for some reason im having a problem of the form not showing up...

 

When I load the page it gives me this error:

UHQqZ.png

 

The lines of code its reffering too is:

<script language="javascript" type="text/javascript">
function showvalue(arg) {
alert(arg);
//arg.visible(false);
}
$(document).ready(function() {
try {
	oHandler = $(".mydds").msDropDown().data("dd");
	oHandler.visible(true);
	//alert($.msDropDown.version);
	//$.msDropDown.create("body select");
	$("#ver").html($.msDropDown.version);
	} catch(e) {
	alert("Error: "+e.message);
	}
})	
</script>

 

The thing is, when I remove that code that we just worked on together the problem goes away... why is this happening?

 

 

Link to comment
Share on other sites

Okay sorry I managed to fix that part, i moved it below some other code and it now gives me an error like this:

Notice: Undefined variable: picture in C:\wamp\www\updat5.php on line 145

 

This is line 145:

echo $picture[2];

 

and its part of this code:

 

<?php

while($row = mysql_fetch_assoc($result))
{
$id = $row['id'];

switch ($row['icon']) {
    case 1:
        $picture[$id] = '<img src="img/apple.gif" title="apple" alt="apple" />';
        echo $picture[$id];
        break;
        
    case 2:
        $picture[$id] = '<img src="img/banana.gif" title="banana" alt="banana" />';
        echo $picture[$id];
        break;
        
    case 3:
        $picture[$id] = '<img src="img/orange.gif" title="orange" alt="orange" />';
        echo $picture[$id];
        break;
        
    default:
        $picture[$id] = '';
        echo $row['icon'] . " is something other than 1 2 or 3";
        break;
}  
}


<?php
echo $picture[2];
?>

 

Link to comment
Share on other sites

Okay, I posted it outside the while and now it gives me a new error:

 

Notice: Undefined offset: 2 in C:\wamp\www\updat5.php on line 147

 

<?php
$picture = array();
while($row = mysql_fetch_assoc($result))
{
$id = $row['id'];
$picture = array();
switch ($row['icon']) {
    case 1:
        $picture[$id] = '<img src="img/apple.gif" title="apple" alt="apple" />';
        echo $picture[$id];
        break;
        
    case 2:
        $picture[$id] = '<img src="img/banana.gif" title="banana" alt="banana" />';
        echo $picture[$id];
        break;
        
    case 3:
      $picture[$id] = '<img src="img/orange.gif" title="orange" alt="orange" />';
        echo $picture[$id];
        break;
        
    default:
        $picture[$id] = '';
        echo $row['icon'] . " is something other than 1 2 or 3";
        break;
}  
}
?>

<?php
echo $picture[2];
?>

 

My next post will be the full code if needed incase the error I have currently cant be fixed with the current snippet/block i posted.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.