Jump to content

Setting a default for a switch loop;


kateevanne

Recommended Posts

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

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>";

}}

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 />

 

 

 

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>";

}}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.