/    Sign up×
Community /Pin to ProfileBookmark

[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.

[code=php]

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
}

[/code]

Thanks in advance for any and all help.

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@NvenomJul 07.2011 — 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

[code=php]
<?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>
[/code]
Copy linkTweet thisAlerts:
@jsnewbie1authorJul 07.2011 — 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!
Copy linkTweet thisAlerts:
@OctoberWindJul 07.2011 — 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.

[code=php]$backgrounds = mysql_query("SELECT * FROM ac WHERE state='Alaska' LIMIT 0,5"); [/code]

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.


[code=php]$backgrounds = mysql_query("SELECT * FROM ac WHERE state='Alaska' LIMIT 6,5"); [/code]
Copy linkTweet thisAlerts:
@jsnewbie1authorJul 08.2011 — 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,
Copy linkTweet thisAlerts:
@NogDogJul 08.2011 — 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.
[code=php]
$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;
[/code]
Copy linkTweet thisAlerts:
@NvenomJul 08.2011 — I'm not sure we can provide a meaningful solution without knowing how you are doing the pagination.[/QUOTE]

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 ?
Copy linkTweet thisAlerts:
@jsnewbie1authorJul 08.2011 — 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...

[code=php]

$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>";

[/code]


Thanks again,
Copy linkTweet thisAlerts:
@NvenomJul 08.2011 — Try this out.

[code=php]
$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>";
[/code]

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

<i>
</i>&lt;style&gt;
body { background: url(../../images/background&lt;?php echo $back_val; ?&gt;.jpg }
&lt;/style&gt;


Also just a side note, if your pagination dose not go above 5 Rows you dont need

[code=php]
$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"; }
[/code]


you could have

[code=php]
$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";
[/code]
Copy linkTweet thisAlerts:
@DerokorianJul 08.2011 — Also if it does go above 5, you could use terniary:
[code=php]
$back_val = ($i<5) ? $i : 5;
[/code]


Which says set back_val to $i when $i is less than 5 otherwise set it to 5.
Copy linkTweet thisAlerts:
@jsnewbie1authorJul 08.2011 — 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,
Copy linkTweet thisAlerts:
@NvenomJul 08.2011 — 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

[code=php]
$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>";
[/code]
Copy linkTweet thisAlerts:
@jsnewbie1authorJul 08.2011 — 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:

[code=php]
$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]




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

[/CODE]
Copy linkTweet thisAlerts:
@NvenomJul 08.2011 — Okay, well going back to your original script, Try this out and tell me if $current_rows gets incremented

[code=php]
$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>";
[/code]


Just checked out your link and saw that it was working ?
Copy linkTweet thisAlerts:
@jsnewbie1authorJul 08.2011 — 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!
Copy linkTweet thisAlerts:
@NvenomJul 08.2011 — Sent you Private message, Also do not forget to mark the Thread Resolved using the Thread tools at the very top
Copy linkTweet thisAlerts:
@jsnewbie1authorJul 08.2011 — Not trying to sound "challenged" but I do not see the resolved button anywhere...?

Nevermind, found it.
×

Success!

Help @jsnewbie1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...