www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: [RESOLVED] Echo Different Backgrounds

  1. #1
    Join Date
    Jun 2011
    Posts
    12

    resolved [RESOLVED] Echo Different Backgrounds

    I am attempting to change the background of a page based off of the number of variables echoed from a Mysql database. I have successfully changed the background using the code below in my page styles, but because I am also using pagination this code understandably only works properly on the first page, every page after would echo the background number 5.

    The code below is what I am currently using, but I know it does not make sense to use it if there is multiple pages unless there is a way to identify how many variables are posted on that specific page... please assist me.

    PHP Code:

    body{ 
    background: url(../../images/background<?php 
    $backgrounds 
    mysql_query("SELECT * FROM ac WHERE state='Alaska'");
    $querygetrownum0 mysql_num_rows($backgrounds);
    if(
    $querygetrownum0==1){echo "1";}
    if(
    $querygetrownum0==2){echo "2";}
    if(
    $querygetrownum0==3){echo "3";}
    if(
    $querygetrownum0==4){echo "4";}
    if(
    $querygetrownum0>=5){echo "5";}
    ?>.jpg
    }
    Thanks in advance for any and all help.

  2. #2
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125
    You might want to explain a bit more, im guessing your changing the image based off of the amount of rows in a DB table.. In theory it would work, Seems very clunky and would require adding rows and changing them and would not work with more than one person using your website at a time... i may just be really confused, but yeah as i said more information please

    Are the people choosing there Location and than the background image is changed based on that choice? If its something like that, just simply add the rows in a DB table with the names and a another row with the value and than pull it up based on there choice.
    The only issue i can see with pagination getting stuck at >=5 is if for some reason its not dumping the echo because pagination dosent refresh the page and is Ajax or something... might want to try instead of echo's, assigning a variable and echoing the variable for the value like this

    PHP Code:
    <?php
    $backgrounds 
    mysql_query("SELECT * FROM ac WHERE state='Alaska'");
    $querygetrownum0 mysql_num_rows($backgrounds);
    if(
    $querygetrownum0==1){$back_val "1";}
    if(
    $querygetrownum0==2){$back_val "2";}
    if(
    $querygetrownum0==3){$back_val "3";}
    if(
    $querygetrownum0==4){$back_val "4";}
    if(
    $querygetrownum0>=5){$back_val "5";}
    ?>

    <style>
    body
    { background: url(../../images/background<?php echo $back_val?>.jpg }
    </style>
    Last edited by Nvenom; 07-07-2011 at 04:42 PM.
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

  3. #3
    Join Date
    Jun 2011
    Posts
    12
    Thanks for the response!

    You definately showed me a cleaner way of accomplishing what the original code accomplished, thanks! But unfortunately I do not believe I was clear on my overall issue.

    The problem is that when pagination creates pages 2-Infinity those pages show the incorrect background. For example, if a blog had 7 posts with a page limit of 5 then there would be a total of two pages to choose from, one with 5 posts and the second with 2 posts. I would like to control the background based off of how many posts are on the page. Page 1 would have the 5th background where the 2nd page would have the second background...

    Another example:

    Pagination #1
    = 1 post, then BG #1 would be seen.
    = 2 post, then BG #2 would be seen.
    = 3 post, then BG #3 would be seen.
    = 4 post, then BG #4 would be seen.
    = 5 post, then BG #5 would be seen.

    Pagination #2
    = 6 post, then BG #1 would be seen.
    = 7 post, then BG #2 would be seen.
    = 8 post, then BG #3 would be seen.
    = 9 post, then BG #4 would be seen.
    = 10 post, then BG #5 would be seen.

    Etc...

    Hopefully my redundant examples make sense, thanks again for your help!
    Last edited by jsnewbie1; 07-07-2011 at 05:25 PM.

  4. #4
    Join Date
    Jul 2007
    Location
    Wisconsin
    Posts
    468
    Are you missing the pagination LIMIT on your query? Since the query is pulling all the rows, its's likely to show the bg img for the "max" numbers of rows, not what's on the page.

    PHP Code:
    $backgrounds mysql_query("SELECT * FROM ac WHERE state='Alaska' LIMIT 0,5"); 
    You would then need to pass the "5+1" value to the query that's on the second page, to know where to start the next query.


    PHP Code:
    $backgrounds mysql_query("SELECT * FROM ac WHERE state='Alaska' LIMIT 6,5"); 
    Last edited by OctoberWind; 07-07-2011 at 06:02 PM. Reason: Wow, still not used to typing on the iPad...

  5. #5
    Join Date
    Jun 2011
    Posts
    12
    October,

    Can you elaborate a little further?

    I currently have a limit but it is on a seperate query that controls the echoed variables... I attempted to use the code you provided and had no luck.

    Thanks,

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,241
    I'm not sure we can provide a meaningful solution without knowing how you are doing the pagination. However, if you know which page you are on and the total number of rows, it would not be hard to calculate with a little basic arithmetic.
    PHP Code:
    $totalRows mysql_num_rows($backgrounds);
    $postsPerPage 5;
    $currentPage 4// have to figure out where to get this value from pagination
    $back_val = ($currentPage <= round($totalRows $postsPerPage))
       ? 
    $num $postsPerPage
       
    $totalRows $postsPerPage
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125
    Quote Originally Posted by NogDog View Post
    I'm not sure we can provide a meaningful solution without knowing how you are doing the pagination.
    Could not have said it better. You need to Find out how your pagination is Totaling up the amount of pages. It should be setting a variable somewhere, Than you just change it based on that variable
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

  8. #8
    Join Date
    Jun 2011
    Posts
    12
    Thanks for the responses!

    Completely understandable. Below is the pagination code that I am using. Apparently I am confused on where in the code I can pull the variable that echos the total number of rows per page...

    PHP Code:

    $per_page 
    5;
    $start $_GET['start'];
    $record_count mysql_num_rows(mysql_query("SELECT * FROM ac WHERE state='Alaska'"));
    $max_pages $record_count $per_page;
            
    if (!
    $start)
    $start 0;    
                    
    $get mysql_query("SELECT * FROM ac WHERE state='Alaska' ORDER BY datetime DESC LIMIT $start$per_page");
    while (
    $row mysql_fetch_assoc($get)){
      
    $city $row['city'];
      echo 
    "$city<br>";}
            
    $prev $start $per_page;
    $next $start $per_page;

    if(!(
    $start<=0))
      echo 
    "<a href='testlead2.php?start=$prev'>Previous</a>";

    $i=1;

    for (
    $x=0;$x<$record_count;$x=$x+$per_page){
    if(
    $start!=$x)
      echo 
    " <a href='ac_alaska.php?start=$x'>$i</a>";
       else
      echo 
    " <a href='ac_alaska.php?start=$x'><b>$i</b></a>";
      
    $i++;}

    if (!(
    $start>=$record_count-$per_page))
      echo 
    "<a href='ac_alaska.php?start=$next'>Next</a>"
    Thanks again,

  9. #9
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125
    Try this out.

    PHP Code:
    $per_page 5;
    $start $_GET['start'];
    $record_count mysql_num_rows(mysql_query("SELECT * FROM ac WHERE state='Alaska'"));
    $max_pages $record_count $per_page;
            
    if (!
    $start)
    $start 0;    
                    
    $get mysql_query("SELECT * FROM ac WHERE state='Alaska' ORDER BY datetime DESC LIMIT $start$per_page");
    while (
    $row mysql_fetch_assoc($get)){
      
    $city $row['city'];
      echo 
    "$city<br>";}
            
    $prev $start $per_page;
    $next $start $per_page;

    if(!(
    $start<=0))
      echo 
    "<a href='testlead2.php?start=$prev'>Previous</a>";

    $i=1;

    for (
    $x=0;$x<$record_count;$x=$x+$per_page){
    if(
    $start!=$x)
      echo 
    " <a href='ac_alaska.php?start=$x'>$i</a>";
       else
      echo 
    " <a href='ac_alaska.php?start=$x'><b>$i</b></a>";
      
    $i++;}

      
    // Pagination Background Change
      
    if($i == 1){ $back_val "1"; }
      if(
    $i == 2){ $back_val "2"; }
      if(
    $i == 3){ $back_val "3"; }
      if(
    $i == 4){ $back_val "4"; }
      if(
    $i >= 5){ $back_val "5"; }

    if (!(
    $start>=$record_count-$per_page))
      echo 
    "<a href='ac_alaska.php?start=$next'>Next</a>"
    I Think that should work.. although i cant really be sure lol without testing it some but anyways tell me how it works.

    and than of course echo the value same as before

    Code:
    <style>
    body { background: url(../../images/background<?php echo $back_val; ?>.jpg }
    </style>
    Also just a side note, if your pagination dose not go above 5 Rows you dont need

    PHP Code:
    $i=1;

    for (
    $x=0;$x<$record_count;$x=$x+$per_page){
    if(
    $start!=$x)
      echo 
    " <a href='ac_alaska.php?start=$x'>$i</a>";
       else
      echo 
    " <a href='ac_alaska.php?start=$x'><b>$i</b></a>";
      
    $i++;}

      
    // Pagination Background Change
      
    if($i == 1){ $back_val "1"; }
      if(
    $i == 2){ $back_val "2"; }
      if(
    $i == 3){ $back_val "3"; }
      if(
    $i == 4){ $back_val "4"; }
      if(
    $i >= 5){ $back_val "5"; } 
    you could have

    PHP Code:
    $i=1;

    for (
    $x=0;$x<$record_count;$x=$x+$per_page){
    if(
    $start!=$x)
      echo 
    " <a href='ac_alaska.php?start=$x'>$i</a>";
       else
      echo 
    " <a href='ac_alaska.php?start=$x'><b>$i</b></a>";
      
    $i++;}

      
    // Pagination Background Change
      
    $back_val "$i"
    Last edited by Nvenom; 07-08-2011 at 02:59 AM.
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

  10. #10
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    Also if it does go above 5, you could use terniary:
    PHP Code:
    $back_val = ($i<5) ? $i 5
    Which says set back_val to $i when $i is less than 5 otherwise set it to 5.
    ~Ryan
    www.rdennispallas.com <-- Personal Site, changing regularly cuz its ugly.

  11. #11
    Join Date
    Jun 2011
    Posts
    12
    When I echo out $i it shows pages + 1 = 4. It is basing the background off of how many pages of 5 are present vs. how many rows on a current page.

    I upload the code to http://roofnc.com/testlead2.php. If you view source the background image is background4.jpg on all three pages, but it should be background5.jpg on the first two pages and background1.jpg on the 3rd page.

    Thanks again,

  12. #12
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125
    I see the issue, But i need to see the entire <?php ?> code that the pagination is in, as i can not view your php on your example..

    The problem is that your script is looping city rows to show them i just dont see the loop anywhere in there. Anyways find the loop and we can place a variable to take advantage of it.

    *EDIT* I understand it now, your not looping your basing it off a limit try this

    PHP Code:
    $per_page 5;
    $start $_GET['start'];
    $record_count mysql_num_rows(mysql_query("SELECT * FROM ac WHERE state='Alaska'"));
    $max_pages $record_count $per_page;
            
    if (!
    $start)
    $start 0;    
                    
    $query="SELECT * FROM ac WHERE state='Alaska' ORDER BY datetime DESC";
    $result=mysql_query($query);

    $c=0;
    while (
    $c <= $per_page) {

    $city=mysql_result($result,$c,"city");
    echo 
    "$city<br>";

    $c++;
    }

    // Pagination Background Change
    if($c == 1){ $back_val "1"; }
    if(
    $c == 2){ $back_val "2"; }
    if(
    $c == 3){ $back_val "3"; }
    if(
    $c == 4){ $back_val "4"; }
    if(
    $c >= 5){ $back_val "5"; }
            
    $prev $start $per_page;
    $next $start $per_page;

    if(!(
    $start<=0))
      echo 
    "<a href='testlead2.php?start=$prev'>Previous</a>";

    $i=1;

    for (
    $x=0;$x<$record_count;$x=$x+$per_page){
    if(
    $start!=$x)
      echo 
    " <a href='ac_alaska.php?start=$x'>$i</a>";
       else
      echo 
    " <a href='ac_alaska.php?start=$x'><b>$i</b></a>";
      
    $i++;}

    if (!(
    $start>=$record_count-$per_page))
      echo 
    "<a href='ac_alaska.php?start=$next'>Next</a>"
    Last edited by Nvenom; 07-08-2011 at 02:28 PM.
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

  13. #13
    Join Date
    Jun 2011
    Posts
    12
    Unfortunately... its still not working properly with your code, now their are six cities showing and they do not change when I click on the pages. Currently I have 11 cities that should echo out in pages of 5.

    The code being used is the same:

    PHP Code:
    $per_page 5;
    $start $_GET['start'];
    $record_count mysql_num_rows(mysql_query("SELECT * FROM ac WHERE state='Alaska'"));
    $max_pages $record_count $per_page;
            
    if (!
    $start)
    $start 0;    
                    
    $query="SELECT * FROM ac WHERE state='Alaska' ORDER BY datetime DESC";
    $result=mysql_query($query);

    $c=0;
    while (
    $c <= $per_page) {

    $city=mysql_result($result,$c,"city");
    echo 
    "$city<br>";

    $c++;
    }

    // Pagination Background Change
    if($c == 1){ $back_val "1"; }
    if(
    $c == 2){ $back_val "2"; }
    if(
    $c == 3){ $back_val "3"; }
    if(
    $c == 4){ $back_val "4"; }
    if(
    $c >= 5){ $back_val "5"; }
            
    $prev $start $per_page;
    $next $start $per_page;

    if(!(
    $start<=0))
      echo 
    "<a href='testlead2.php?start=$prev'>Previous</a>";

    $i=1;

    for (
    $x=0;$x<$record_count;$x=$x+$per_page){
    if(
    $start!=$x)
      echo 
    " <a href='testlead2.php?start=$x'>$i</a>";
       else
      echo 
    " <a href='testlead2.php?start=$x'><b>$i</b></a>";
      
    $i++;}

    if (!(
    $start>=$record_count-$per_page))
      echo 
    "<a href='testlead2.php?start=$next'>Next</a>"


    Code:
    <style type="text/css">
    body{ 
    background: url(../../images/background<?php echo $back_val; ?>.jpg) 
    } 
    </style>

  14. #14
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125
    Okay, well going back to your original script, Try this out and tell me if $current_rows gets incremented

    PHP Code:
    $per_page 5;
    $start $_GET['start'];
    $record_count mysql_num_rows(mysql_query("SELECT * FROM ac WHERE state='Alaska'"));
    $max_pages $record_count $per_page;
            
    if (!
    $start)
    $start 0;    

    $current_rows 0;             
    $get mysql_query("SELECT * FROM ac WHERE state='Alaska' ORDER BY datetime DESC LIMIT $start$per_page");
    while (
    $row mysql_fetch_assoc($get)){
      
    $city $row['city'];
      
    $current_rows $current_rows 1;
      echo 
    "$current_rows$city<br>";}

    // Pagination Background Change
    if($current_rows == 1){ $back_val "1"; }
    if(
    $current_rows == 2){ $back_val "2"; }
    if(
    $current_rows == 3){ $back_val "3"; }
    if(
    $current_rows == 4){ $back_val "4"; }
    if(
    $current_rows >= 5){ $back_val "5"; }
            
    $prev $start $per_page;
    $next $start $per_page;

    if(!(
    $start<=0))
      echo 
    "<a href='testlead2.php?start=$prev'>Previous</a>";

    $i=1;

    for (
    $x=0;$x<$record_count;$x=$x+$per_page){
    if(
    $start!=$x)
      echo 
    " <a href='testlead2.php?start=$x'>$i</a>";
       else
      echo 
    " <a href='testlead2.php?start=$x'><b>$i</b></a>";
      
    $i++;}

    if (!(
    $start>=$record_count-$per_page))
      echo 
    "<a href='testlead2.php?start=$next'>Next</a>"
    Just checked out your link and saw that it was working
    Last edited by Nvenom; 07-08-2011 at 03:47 PM.
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

  15. #15
    Join Date
    Jun 2011
    Posts
    12
    PERFECT!!! Thanks for all of your help... and your patience!

    If you have a paypal account I will through a "tip" into it.

    Thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles