kateevanne Posted May 10, 2009 Share Posted May 10, 2009 Hi All, I am getting an error message when I run my page Notice: Undefined index: category in /Applications/MAMP/htdocs/PROJECT/attemptagain/main_login.php on line 53 I know why its happening, I have a switch conditional sorting entries by category, but when these links are not clicked 'category' has no value. I want my default page to show the most recent posts rather than sort by category, so I have made this an 'elseif', and its working fine, but is there a way I can get rid of this error message- it just appears at the top all the time? I have tried a few things but no joy! Thanks Kate Link to comment https://forums.phpfreaks.com/topic/157617-setting-a-default-for-a-switch-loop/ Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 I'm assuming category is an array? Can we see some code? Link to comment https://forums.phpfreaks.com/topic/157617-setting-a-default-for-a-switch-loop/#findComment-831108 Share on other sites More sharing options...
kateevanne Posted May 10, 2009 Author Share Posted May 10, 2009 if ($category = $_GET['category']){ switch ($category) { case "landscape": $query = "SELECT * FROM $tbl2_name WHERE category='landscape'"; break; case "portrait": $query = "SELECT * FROM $tbl2_name WHERE category='portrait'"; break; case "hdr": $query = "SELECT * FROM $tbl2_name WHERE category='hdr'"; break; } $result = mysql_query($query); while($row = mysql_fetch_array($result)) { echo $row['title']; echo "<a href=\"photo.php?id=".$row['id']."\"><img src={$row['file']}/></a><br>"; }} else { $query ="SELECT * FROM $tbl2_name WHERE category='landscape' ORDER BY time DESC LIMIT 1"; if ($r = mysql_query ($query)) { while ($row = mysql_fetch_array ($r)) { echo $row['title']; echo "<a href=\"photo.php?id=".$row['id']."\"><img src={$row['file']}/></a><br>"; } } $query ="SELECT * FROM $tbl2_name WHERE category='portrait' ORDER BY time DESC LIMIT 1"; if ($r = mysql_query ($query)) { while ($row = mysql_fetch_array ($r)) { echo $row['title']; echo "<a href=\"photo.php?id=".$row['id']."\"><img src={$row['file']}/></a><br>"; } } } $query ="SELECT * FROM $tbl2_name WHERE category='hdr' ORDER BY time DESC LIMIT 1"; if ($r = mysql_query ($query)) { while ($row = mysql_fetch_array ($r)) { echo $row['title']; echo "<a href=\"photo.php?id=".$row['id']."\"><img src={$row['file']}/></a><br>"; }} Link to comment https://forums.phpfreaks.com/topic/157617-setting-a-default-for-a-switch-loop/#findComment-831111 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Which is line 53? Link to comment https://forums.phpfreaks.com/topic/157617-setting-a-default-for-a-switch-loop/#findComment-831116 Share on other sites More sharing options...
kateevanne Posted May 10, 2009 Author Share Posted May 10, 2009 Line 53 is if ($category = $_GET['category']){ the categories are set using these links: <a href="main_login.php?category=landscape">Landscape</a><br /> <a href="main_login.php?category=portrait">Portrait</a><br /> <a href="main_login.php?category=hdr">HDR</a><br /> Link to comment https://forums.phpfreaks.com/topic/157617-setting-a-default-for-a-switch-loop/#findComment-831120 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 So the first time you view this main_login.php page, does it have a category in the URL or not? Link to comment https://forums.phpfreaks.com/topic/157617-setting-a-default-for-a-switch-loop/#findComment-831124 Share on other sites More sharing options...
kateevanne Posted May 10, 2009 Author Share Posted May 10, 2009 no the first time it doesn't. Link to comment https://forums.phpfreaks.com/topic/157617-setting-a-default-for-a-switch-loop/#findComment-831126 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Well there you go. $_GET['category'] wouldn't work the first time because there is no category index in the $_GET array. Try this - if (!empty($_GET['category'])) { $category = $_GET['category']; switch ($category) { case "landscape": $query = "SELECT * FROM $tbl2_name WHERE category='landscape'"; break; case "portrait": $query = "SELECT * FROM $tbl2_name WHERE category='portrait'"; break; case "hdr": $query = "SELECT * FROM $tbl2_name WHERE category='hdr'"; break; } $result = mysql_query($query); while($row = mysql_fetch_array($result)) { echo $row['title']; echo "<a href=\"photo.php?id=".$row['id']."\"><img src={$row['file']}/></a><br>"; }} else { $query ="SELECT * FROM $tbl2_name WHERE category='landscape' ORDER BY time DESC LIMIT 1"; if ($r = mysql_query ($query)) { while ($row = mysql_fetch_array ($r)) { echo $row['title']; echo "<a href=\"photo.php?id=".$row['id']."\"><img src={$row['file']}/></a><br>"; } } $query ="SELECT * FROM $tbl2_name WHERE category='portrait' ORDER BY time DESC LIMIT 1"; if ($r = mysql_query ($query)) { while ($row = mysql_fetch_array ($r)) { echo $row['title']; echo "<a href=\"photo.php?id=".$row['id']."\"><img src={$row['file']}/></a><br>"; } } } $query ="SELECT * FROM $tbl2_name WHERE category='hdr' ORDER BY time DESC LIMIT 1"; if ($r = mysql_query ($query)) { while ($row = mysql_fetch_array ($r)) { echo $row['title']; echo "<a href=\"photo.php?id=".$row['id']."\"><img src={$row['file']}/></a><br>"; }} Link to comment https://forums.phpfreaks.com/topic/157617-setting-a-default-for-a-switch-loop/#findComment-831136 Share on other sites More sharing options...
kateevanne Posted May 10, 2009 Author Share Posted May 10, 2009 Thank you so Much! I was tring to do that all wrong- very new to php- its great now! Link to comment https://forums.phpfreaks.com/topic/157617-setting-a-default-for-a-switch-loop/#findComment-831139 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.