简体   繁体   中英

how to change website background using php and mysql?

i have an issue changeing the background , it always show or change the background only if the image id =1 , how can i dysplay other values ?

here is my php code :

<?php
include "config.php"; //Database connection file
//Check the database for background color
$check_page_color = mysql_query("select * from `page_color` where `id` = '".mysql_real_escape_string('1')."'"); // where `id` = '".mysql_real_escape_string('1')."'
if(mysql_num_rows($check_page_color) > 1) //If no color is found, use the default color below > 1
{
$page_color = "#F9F9F9"; //Default Color
}
else
{
    $get_page_color = mysql_fetch_array($check_page_color);
    $page_color = strip_tags($get_page_color["background_color"]); //Color from the database
}
?>

CSS :

<style type="text/css">

body {  
background-image: url(<?php echo $page_color; ?>);
</style>

and here is the HTML code that displays the photos :

New Background:<select name="img" class="vpb_field" id="color" style="height: 40px; width: 151px" >
                <option><?php echo $page_color; ?></option>
            </select>
<span class="vpb_general_button" onclick="vpb_submit_form();">Submit</span>

Why you taking 1 id in mysql you can take all with while and add them in a string[] and again you can make new while on option for all color options

<?php $page_color = mysql_query("select * from `page_color`"); 
  $counter = 0;
  $i = 0;
  while($showall = mysql_fetch_array($page_color))
        {
        extract($showall);//<-- we cut your data hire
        $counter++;
        $array_colors[] = $color; // <-- This is your mysql col
        ?>
        <select name="img" class="vpb_field" id="color" style="height: 40px; width: 151px" >
       <?php while($i < $counter) // <-- You can change this if u want size_of     
         {echo '<option>'. $array_colors[$i] .'</option>';}
         ?>
        </select>

this will give you all options now ucan use your other html code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM