Russia Posted February 1, 2011 Share Posted February 1, 2011 Im having this error and have the not even the slightest clue on how to solve it... Notice: Undefined offset: 2 in C:\wamp\www\updat5.php on line 146 <?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; } } ?> <hr> <?php echo $picture[2]; ?> And line 146 is: 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. Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/ Share on other sites More sharing options...
AbraCadaver Posted February 1, 2011 Share Posted February 1, 2011 You have 2 $picture = array(); Get rid of the second one, you don't want to empty your array each time through the loop. Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168598 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 I did get rid of one, but the error still remains... why is that? Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168600 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 Updated code: <?php $picture = array(); 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; } } ?> and this is the part that echos that shows the images: <?php echo $picture[2]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168601 Share on other sites More sharing options...
KevinM1 Posted February 1, 2011 Share Posted February 1, 2011 The error is happening because you're attempting to access an array element that doesn't exist. Your array has one item, indexed by whatever value $id contains. $picture[2] is attempting to access the third (because arrays are zero-indexed) item in the array, which, again, doesn't exist. Hence an undefined offset. There's a simple fix: $picture['2'] Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168603 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 Okay, I fixed that by adding the ' ' parts. Now it says: Notice: Undefined index: 2 in C:\wamp\www\updat5.php on line 144 Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168604 Share on other sites More sharing options...
KevinM1 Posted February 1, 2011 Share Posted February 1, 2011 Actually, I was wrong originally. Arrays in other languages act differently than PHP arrays. Still, the point remains, are you sure you have an $id of 2? Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168606 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 Yes sir. Everything worked before when I had it on another part of the website, but I moved it down since it was interfering with the javascript. Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168607 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 I had this error: So then I moved the code under the table with the form and it stopped working. But you can see the \"banana\" in the image showing the echoing of the row value. so somehow its not working once i put it under the form. this is the lines of code the error was referring to: <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> Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168608 Share on other sites More sharing options...
KevinM1 Posted February 1, 2011 Share Posted February 1, 2011 Can you show more code? It's hard to diagnose without seeing how things are called. Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168609 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 Il post the whole script for you here: <?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']; $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]; if(!($result1 = mysql_query($sql1))) { "<BR>Error UPDATING $tbl_name "; exit(); } } } $result=mysql_query($sql); $count=mysql_num_rows($result); ?> // ORIGINAL SPOT OF THE CODE THAT WORKED PERFECTLY BUT THAT GAVE THE popup ERROR! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Image Dropdown</title> <link rel="stylesheet" type="text/css" href="dd.css" /> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> <script type="text/javascript" src="jquery.dd.js"></script> </head> <body> <form name="form1" method="post" action="updat5.php"> <tr> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>ID</strong></td> <td align="center"><strong>Month Date</strong></td> <td align="center"><strong>Message</strong></td> <td align="center"><strong>Title</strong></td> <td align="center"><strong>Icon</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?> <input type="hidden" id="id" name="id[]" value="<?php echo $rows['id']; ?>"> </td> <td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="month[]" MAXLENGTH="3" size="3" type="text" id="month" value="<?php echo $rows['month']; ?>"> <input style="border: 1px solid #C3C3C3;height: 20px;" name="date[]" MAXLENGTH="2" size="2" type="text" id="date" value="<?php echo $rows['date']; ?>"> </td> <td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="message[]" size="70" type="text" id="message" value="<?php echo $rows['message']; ?>"></td> <td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="title[]" size="70" type="text" id="title" value="<?php echo $rows['title']; ?>"></td> <td align="center"> <select name="icon[]" style="width:200px" class="mydds"> <option value="1"<?php if ($rows['icon'] == 1) { echo 'selected="selected"'; } ?> title="icon/icon_phone.gif">Phone</option> <option value="2"<?php if ($rows['icon'] == 2) { echo 'selected="selected"'; } ?> title="icon/icon_sales.gif">Graph</option> <option value="3"<?php if ($rows['icon'] == 3) { echo 'selected="selected"'; } ?> title="icon/icon_faq.gif">Faq</option> </select> </td> </tr> <?php } ?> <tr> <td colspan="4" align="center"><br><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </td> </tr> </form> <hr> //SO I MOVED IT TO THIS SPOT AND IT STOPPED WORKING AND SAYING ITS MISSING VARIABLES AND INDEXES. <?php $picture = array(); 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']; ?> <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> </body> </html> If you look in the code I made 2 //comments about the positions of the code at different times. Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168611 Share on other sites More sharing options...
KevinM1 Posted February 1, 2011 Share Posted February 1, 2011 Do you have a link I could visit? I'd like to see the rendered HTML. Also, which code did you move, and to where? I can't tell if you're talking about the JavaScript, some of the PHP, or both. Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168614 Share on other sites More sharing options...
Russia Posted February 1, 2011 Author Share Posted February 1, 2011 Im talking about this code I moved: <?php $picture = array(); 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']; ?> Also, no I dont have a link because as you an see all my errors are pointing to: C:\wamp\www\updat5.php Its on my wamp server. But yeah, do you want me to make a video of the error live? You tell me what you want me to record and il post a video on youtube and give you the link. [/code] Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168616 Share on other sites More sharing options...
KevinM1 Posted February 2, 2011 Share Posted February 2, 2011 I cleaned up your code a bit. You had some redundancy, and your update query needed tweaking (you were inadvertently overwriting values, and then trying to access scalar variables as array variables). I'm as baffled as you, to be honest. The only answer I can see is that you don't have an $id which equals 2. Double check your db to make sure you have a row whose id is 2. You could also do a simple test: $isIDGood = (isset($picture[2])) ? "yes" : "no"; echo 'Is $picture[2] valid? ' . $isIDGood; And, really, it's good practice to check to see if a particular array element is set before calling it directly anyway. --- Anyway, here's a slightly cleaned up version for you: <?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'][$i]; $date[] = $_POST['date'][$i]; $message[] = $_POST['message'][$i]; $title[] = $_POST['title'][$i]; $id[] = $_POST['id'][$i]; $icon[] = $_POST['icon'][$i]; $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]}'"; if(!($result1 = mysql_query($sql1))){ "<BR>Error UPDATING $tbl_name "; exit(); } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Image Dropdown</title> <link rel="stylesheet" type="text/css" href="dd.css" /> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> <script type="text/javascript" src="jquery.dd.js"></script> </head> <body> <form name="form1" method="post" action="updat5.php"> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>ID</strong></td> <td align="center"><strong>Month Date</strong></td> <td align="center"><strong>Message</strong></td> <td align="center"><strong>Title</strong></td> <td align="center"><strong>Icon</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center"> <?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?><input type="hidden" id="id" name="id[]" value="<?php echo $rows['id']; ?>"> </td> <td align="center"> <input style="border: 1px solid #C3C3C3;height: 20px;" name="month[]" MAXLENGTH="3" size="3" type="text" id="month" value="<?php echo $rows['month']; ?>"> <input style="border: 1px solid #C3C3C3;height: 20px;" name="date[]" MAXLENGTH="2" size="2" type="text" id="date" value="<?php echo $rows['date']; ?>"> </td> <td align="center"> <input style="border: 1px solid #C3C3C3;height: 20px;" name="message[]" size="70" type="text" id="message" value="<?php echo $rows['message']; ?>"> </td> <td align="center"> <input style="border: 1px solid #C3C3C3;height: 20px;" name="title[]" size="70" type="text" id="title" value="<?php echo $rows['title']; ?>"> </td> <td align="center"> <select name="icon[]" style="width:200px" class="mydds"> <option value="1"<?php if ($rows['icon'] == 1) { echo 'selected="selected"'; } ?> title="icon/icon_phone.gif">Phone</option> <option value="2"<?php if ($rows['icon'] == 2) { echo 'selected="selected"'; } ?> title="icon/icon_sales.gif">Graph</option> <option value="3"<?php if ($rows['icon'] == 3) { echo 'selected="selected"'; } ?> title="icon/icon_faq.gif">Faq</option> </select> </td> </tr> <?php } ?> <tr> <td colspan="4" align="center"> <br><input type="submit" name="Submit" value="Submit"> </td> </tr> </table> </form> <hr> <?php $picture = array(); 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]; ?> <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> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168622 Share on other sites More sharing options...
Russia Posted February 2, 2011 Author Share Posted February 2, 2011 I do have one here is a pic of my db: Also, il try that code you posted that checks if it shows or not. Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1168627 Share on other sites More sharing options...
KevinM1 Posted February 2, 2011 Share Posted February 2, 2011 Why don't you have your icon data stored in another table? You have a one-to-many relationship there: one icon can have many related entities. Instead of manually creating the relationship with a PHP switch, put your icon info in a separate table containing an id and path to the icon itself, and run a JOIN to retrieve the aggregated info. That would save you a lot of work in your script, as you wouldn't have to build an entirely new array just to display the icons. So, you'd keep your current table, and create another for the icons where the 'icon' column of test_mysql is a foreign key containing the id/primary key value of the actual icon table. For more info, take a look at: http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html Quote Link to comment https://forums.phpfreaks.com/topic/226406-notice-undefined-offset-error-no-idea-why-its-happening/#findComment-1169014 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.