Jump to content

Is this a case for SWITCH?


R0CKY

Recommended Posts

PHP clueless here...

What is the best way to achieve this requirement?

Check a database field and retrieve a value $category
echo an image depending the value of $category

The value of $category can be anything from 1-40

For example

If $category = 1,2,10 or 14 then echo image catyellow.gif
If $category = 3,7,11 or 12 then echo image catred.gif
If $category = 20,21,31 or 32 then echo image catblue.gif

etc etc

Is this a case for using the [b]switch[/b] command, or is there another (clever) way to achieve the same thing but with less code. For example perhaps by naming the gifs to accord with the $category values...?

Thanks for any hints.
Link to comment
Share on other sites

Where is $category coming from? If it is coming from the database you could created a table with cat_id and cat_img, then pull that information when you do the query to get the $category variable.

If not, then switch is a good solution and you can combine several values for one image.

Here's another approach:
[code]$categoryAry['|1|2|10|14|'] = "catyellow.gif";
$categoryAry['|3|7|11|12|'] = "catred.gif";

foreach ($category as $cats => $aryImg) {
  if (substr_count($categoryAry, "|".$category."|")>0) {
    $image = $aryImg;
  }
}[/code]
Link to comment
Share on other sites

[quote author=mjdamato link=topic=121048.msg497098#msg497098 date=1167949017]
Where is $category coming from?
[/quote]

Yes it is coming from the database, as you can see in the following loop. At the moment I am simply getting the loop to echo the category (as a number), but as per my request above, I am now trying to assign a relevant image and have it appear instead.

[quote]
$i = 1;
while ($file = mysql_fetch_object($result)) {
[b]$cat = ($file->file_catid);[/b]
$line = str_replace("{number}", $i, $template);
$line = str_replace("{filelink}", "$config[3]/pafiledb.php?action=file&id=$file->file_id", $line);
$line = str_replace("{filename}", trimlen($file->file_name), $line);
    //$line = str_replace("{filename}", $file->file_name, $line);
if ($list == "newest") {  $infoline = str_replace("{date}", date("n/j/y", $file->file_time), $info); }
if ($list == "downloads") {$infoline = str_replace("{downloads}", $file->file_dls , $info); }
if ($list == "rating") {
$ntv = $file->file_totalvotes - 1;
$infoline = str_replace("{rating}", @round($file->file_rating/$ntv,2), $info);
$infoline = str_replace("{votes}", $ntv, $infoline);
}
$line = str_replace("{info}", $infoline, $line);
[b]echo $cat;[/b]
echo $line;
$i++;
}
?>
[/quote]

I am new to this and don't really understand the code you posted but I think I understand how a switch works, and I'll try doing it with multiple values as you suggested.

Thanks.
Link to comment
Share on other sites

Here is the code for a switch
[code]<?php
switch($category) {
  case: 1
  case: 2
  case: 10
  case: 14
    $pic = "catyellow.gif";
    break;

  case: 3
  case: 7
  case: 11
  case: 12
    $pic = "catred.gif";
    break;

//Add additional cases

}
?>[/code]
Link to comment
Share on other sites

I think, with that, I'd just set the categories into an array and use an "if" statement for it still:
[code]
<?php
if (in_array(array(1, 2, 10, 14))) $pic = 'catyellow.gif';
else if (in_array(array(3, 7, 11, 12))) $pic = 'catred.gif';
?>
[/code]
Link to comment
Share on other sites

I like the array idea, but don't know how to make a category array as suggested, so I went with the longer switch code, but it's not working, there is no output from the entire page with the following code inserted. If I comment out the switch section, the page works again. 

[quote] $cat = ($file->file_catid);

  switch($cat) {
  case: 1
  case: 2
  case: 10
  case: 14
    echo "<img src=\"../images_site/mods-gr1.jpg\" title=\"GR1\" />";
    break;
  case: 3
  case: 7
  case: 11
  case: 12
    echo "<img src=\"../images_site/mods-graw1.jpg\" title=\"GRAW1\" />";
    break;
  default:
    echo "<img src=\"../images_site/mods-gen.jpg\" title=\"GEN\" />";

//Add additional cases

}[/quote]

I really can't see what is wrong with the above, I even tried changing the img src code to echo simple piece of text, and there was still no output at all.

Any ideas?  :-\
Link to comment
Share on other sites

There are syntax errors in the switch statement. On each "case" the ":" should be after the value:
[code]<?php
  $cat = ($file->file_catid);

  switch($cat) {
  case 1:
  case 2:
  case 10:
  case 14:
    echo "<img src=\"../images_site/mods-gr1.jpg\" title=\"GR1\" />";
    break;
  case 3:
  case 7:
  case 11:
  case 12:
    echo "<img src=\"../images_site/mods-graw1.jpg\" title=\"GRAW1\" />";
    break;
  default:
    echo "<img src=\"../images_site/mods-gen.jpg\" title=\"GEN\" />";
}?>[/code]

No, you don't need a "break" in the default case, since it's usually the last one in the list.

Ken
Link to comment
Share on other sites

It works fantastic now, the result is really great, but the code has turned out a bit over the top - and it's not even finished yet... take a look..

[quote] $cat = ($file->file_catid);

  switch($cat) {
  case 15:
  case 16:
  case 17:
  case 18:
  case 19:
  case 20:
  case 21:
  case 22:
  case 23:
  case 24:
  case 28:
  case 30:
  case 31:
  case 43:
    echo "<img src=\"../images_site/mods-gr1.jpg\" title=\"GR1\" />";
    break;
  case 27:
  case 33:
  case 44:
  case 41:
  case 36:
  case 37:
  case 39:
  case 40:
  case 34:
  case 35:
  case 38:
  case 42:
  case 45:
    echo "<img src=\"../images_site/mods-graw1.jpg\" title=\"GRAW1\" />";
    break;
  default:
    echo "<img src=\"../images_site/mods-gen.jpg\" title=\"GEN\" />";

//Add additional cases

}[/quote]

Can anyone give me some (beginner) help with perhaps doing this by array instead?

Many thanks.
Link to comment
Share on other sites

I think it may help you if you simply had a separate dabase table of images each with their own id. When you grab teh cat-id of the file I assukme you are grabbing that from a database. If that is the case then on teh table that stores teh cat_id information add a field to store the id of teh image that should be used.

then you coudl do this.

[code]
<?php
$pic_id = ($file->file_picid);

$qry = "SELECT * FROM `images` WHERE `img_id` = " . $pic_id ."";
$qry = mysql_query($qry);
$row = mysql_fetch_assoc($qry);
echo "<img src=\"" . $row['img_path'] . "\" title=\"GEN\" />";
?>
[/code]

It may involve one more query but very extenable.

OR if you trust people to do the right thing same thing without the table for images. In the table that stores the cat_id again another field just to hold a number.

save the different images as something like 'mods_1.jpg'

then all you'd need is:
[code]
<?php
$pic_id = ($file->file_picid);
echo "<img src=\"../images_site/mods_" . $pic_id . ".jpg\" title=\"GEN\" />";

?>
[/code]

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.