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
}
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
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!
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...
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
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
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>";}
$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>";}
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
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.
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);
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
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);
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
Bookmarks