Russia Posted February 1, 2011 Share Posted February 1, 2011 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 https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/ Share on other sites More sharing options...
Pikachu2000 Posted February 1, 2011 Share Posted February 1, 2011 The value of $rows['icon'] must equal something other than 1, 2 or 3. Have you tried echoing it to see what it actually is? Nothing in the second code chunk assigns a value to $rows['icon'], BTW. Link to comment https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168133 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 Yeah here is a picture of my database. Can you help me fix this issue? Link to comment https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168134 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 Yeah I have no idea on how to make this work with multiple rows... Link to comment https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168139 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 Somone told me to do this "set $picture to an array and when you call on the picture just put the number you want (IE 5 items - $picture[0] thru $picture[4])" I really dont understand how to do that. Link to comment https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168141 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 bump. please help. Link to comment https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168154 Share on other sites More sharing options...
Pikachu2000 Posted February 1, 2011 Share Posted February 1, 2011 Do not bump threads. Link to comment https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168157 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 Sorry mate, but i really am not understanding this. Can you help me out? not being rude mate Link to comment https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168158 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 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 https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168538 Share on other sites More sharing options...
AbraCadaver Posted February 1, 2011 Share Posted February 1, 2011 switch ($row['icon']) { Not: switch ($result['icon']) { Link to comment https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168544 Share on other sites More sharing options...
AbraCadaver Posted February 1, 2011 Share Posted February 1, 2011 This will tell you that: error_reporting(E_ALL); ini_set('display_errors', '1'); Link to comment https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168549 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 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 https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168555 Share on other sites More sharing options...
AbraCadaver Posted February 1, 2011 Share Posted February 1, 2011 $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 https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168563 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 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: 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 https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168568 Share on other sites More sharing options...
AbraCadaver Posted February 1, 2011 Share Posted February 1, 2011 Needs to go in the javascript forum (looks like jquery?). Link to comment https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168571 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 Yeah, I was just wondering if you really had any idea haha. Okay il post it there. The thing is, what should i post? my whole code, or just the part we worked on? Link to comment https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168573 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 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 https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168576 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 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 https://forums.phpfreaks.com/topic/226300-mysql-switch-statement-doesnt-work-right/#findComment-1168590 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.