Jump to content

PHP & CSS Switch Question


bgnm2000

Recommended Posts

Hey everyone,

I'm a complete newb when it comes to PHP, so I apologize if I sound like a bozo asking this.

 

I'm trying to edit a script I have to show search results in varying color, I.E. one gray, one white, one gray, one white etc.

 

A friend told me this can be done through a switch and loop. If I could see a quick demonstration (the code) of something like this, I'm pretty sure I'd be able to figure out the rest.

 

thanks for your time! and thanks in advance for any help!

 

-bgnm

Link to comment
https://forums.phpfreaks.com/topic/89466-php-css-switch-question/
Share on other sites

well you can use a variety of methods depending on how

you coded it, can you show us some code so that

we can help you edit it?

because if you are going to display information using

a loop such as foreach() or while() you can just

do something like this:

 

<table>
<?php
$x = 1;
// lets say you have a foreach() loop
foreach(/* Your Data*/){
if($x == 1){
$bg = "background-color: #888888;";
$x++;
} else{
$bg = "background-color: #FFFFFF;";
$x--;
}
echo '<tr style="'. $bg .'"><td>Data</td></tr>';
}
?>
</table>

the current code in this script is:

 

function display_list($listing) {

$color1		= 'listing';
$color2		= 'listing_alt';
$row_count 	= 0;

if ($listing) {
	foreach($listing as $key => $value) {

		$row_color = ($row_count % 2) ? $color1 : $color2;



    echo '<div id="listing">';
        echo '<div class="left" id="listing_title"><span class="right"><a href="'.SITE_URL.'index.php?userid='.$listing[$key]["owner_id"].'">' . $listing[$key]["company"].'</a></span><a href="'.SITE_URL."job.php?job=".$listing[$key]['id'].'"><strong>' . $listing[$key]["title"].'</strong></a></div>';
        echo '<div id="listing_details">';
             		$my_desc = desc_truncate($listing[$key]['description'], 100);

          echo '<span class="style1">('.LANG_LISTING_POSTED.' '.$listing[$key]['added'].')</span> '.$listing[$key]["location"].'</div>';
    echo '<div id="listing_description">';
     		
		echo "<span class='listing_description'>".$my_desc."</span>";

		echo "</div>";
     	 echo "</div>";     	

		if ($_SESSION['userid'] == $listing[$key]['owner_id'] || $_SESSION['valid_role'] == 1) {echo " <a href='".SITE_URL."edit.php?job=".$listing[$key]['id']."' class='edit'>".LANG_LISTING_EDIT."</a>";}
		if ($_SESSION['valid_role'] == 1) {
			if ($listing[$key]['status'] == 0) {
				echo " | <a href='".SITE_URL."scripts/activate.php?job=".$listing[$key]['id']."'>".LANG_LISTINGS_ACTIVATE_LISTING."</a>";
			}
			else {
				echo " | <a href='".SITE_URL."scripts/suspend.php?job=".$listing[$key]['id']."' class='suspend'>".LANG_LISTING_SUSPEND."</a>";
			}
		}
		echo "</span>";

	$row_count++;
	}
}
else {
	echo '';
}
}

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.