html - while background change php/css


Keywords:php 


Question: 

i've created a php script that connects to my database (mysql) this will be changed to a more secure enviorment.

But for now i want to change the bg color of my out come

        'echo  "<h2><a href=''>$title</a></h2>
                <b>$description</b><br  /> ";


            }

        }
        else
            echo "Geen resultaat gevonden voor \"<b>$s</b>\""; '

im not sure how to do so with a while loop.

 <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
    <title> Snuffelen</title>
</head>
<body>
        <a href="">
  <img src="" alt="Snuffelen logo" style="width:250px;height:75px;border:0;">
</a>
        <form action='./search.php' method='get'>
            <input type='text' name='s'size='50' value='<?php echo $_GET['s']; ?>' />
            <input type='submit' value='Zoek'/>
        </form>
        <hr />
        <?php   

            $s = $_GET['s'];            
            $terms = explode (" ", $s);
            $query = "SELECT * FROM ID WHERE ";

            foreach ($terms as $each){
                $i++;

                if ($i ==1)
                    $query .= "keywords LIKE '%$each%' ";
                else
                    $query .= "OR keywords LIKE '%$each%' ";
            }
            //connect to database
            mysql_connect("server", "user", "pw");
            mysql_select_db("DB");

            $query = mysql_query($query);
            $numrows = mysql_num_rows($query);
            if($numrows > 0){

                while($row = mysql_fetch_assoc($query)){
                    $id = $row['id'];
                    $photo = $row['photo'];
                    $title = $row['title'];
                    $description = $row['description'];
                    $price = $row['price'];
                     = $row['Link'];
                    $keywords = $row['keywords'];
                    $euro = $row['euro'];
                    $line = $row ["__________________________________________________________________________________________________"];

                    echo  "<h2><a href=''>$title</a></h2>
                    <b>$description</b><br  /> ";


                }

            }
            else
                echo "Geen resultaat gevonden voor \"<b>$s</b>\"";

            //disconect
            mysql_close();
        ?>
</body>
</html> 

i whould like to add to colors 1 light grey and 2 white and then the next one light grey white light grey etc etc

does anybody know how to do so??


2 Answers: 

Please try this

$bgColor = "style='background-color: green;'";
echo "<div ".$bgColor.">";

  if($numrows > 0){

                while($row = mysql_fetch_assoc($query)){
                    $id = $row['id'];
                    $photo = $row['photo'];
                    $title = $row['title'];
                    $description = $row['description'];
                    $price = $row['price'];
                     = $row['Link'];
                    $keywords = $row['keywords'];
                    $euro = $row['euro'];
                    $line = $row ["__________________________________________________________________________________________________"];

                    echo  "<h2><a href=''>$title</a></h2>
                    <b>$description</b><br  /> ";


                }

            }
            else
                echo "Geen resultaat gevonden voor \"<b>$s</b>\"";
				
		echo "</div>";
 

Please try this

$bgColorGreen = "style='border: 1px solid green;'";
$bgColorBlue = "style='border: 1px solid blue;'";

  if($numrows > 0){
					$i = 0;
                while($row = mysql_fetch_assoc($query)){
                    $id = $row['id'];
                    $photo = $row['photo'];
                    $title = $row['title'];
                    $description = $row['description'];
                    $price = $row['price'];
                     = $row['Link'];
                    $keywords = $row['keywords'];
                    $euro = $row['euro'];
                    $line = $row ["__________________________________________________________________________________________________"];
					
					if($i % 2 == 0){
						echo "<hr ".$bgColorGreen.">";
					}else{
						echo "<hr ".$bgColorBlue.">";
					}

                    echo  "<h2><a href=''>$title</a></h2>
                    <b>$description</b><br  /> ";

$i++;
                }

            }
            else
                echo "Geen resultaat gevonden voor \"<b>$s</b>\"";