• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

anyone know php?

coolguy23

Guest
i need someone to make me a display.php script so that if i want to change the layout i only have to edit that one!

so for example if i link to ../display.php?id=195 it'll show pic number 195.jpg...is that possible?

thanks if you can help!!!
 
That is very possible.

PHP is very flexible when it comes to using the query string. In fact, you don't need any more code...once you define ID to equal a number, that will be recognized within the script.

Here:

Code:
<?php

echo "<img src=$id.gif>";

?>

If you link to display.php?id=492, 492.gif will then be printed to the browser with that code.

You can even setup a little feature that, if no number is specified, the script uses a default one. (Make sure you put this before the code above, still between the <?php and ?> tags, though.)

Code:
if ($id == 0) {
	$id = 1;
}

Easy :)
 
thanks for the help but can you actually make me the scropt because i don't know how to write it...and please tell me where i should put a code if i wanted to put an ad at the top and bottom of the page......thanks for all the help!! :)
 
It gets put into any old HTML file--trick is, you have to rename that file to a .php extension.

You start PHP code with <?php and end it with ?>. Normal HTML can go anywhere but inside those two operators (well, it can, but you have to "echo" or "print" it.)

Anyway, just copy and paste this code where ever you want the image to be displayed.

Code:
<?php

// If no ID is specified, use a default.
if ($id == 0) {
	$id = 1;
}

// Print the image.
echo "<img src=$id.gif>";

?>

Again, you might want to change the default (if nothing is specified) from 1 to whatever.

You can also use .jpg rather than .gif if you need to.

You'll probably have to use Notepad for this--just make sure you save it as display.php (it doesn't really matter, though). Upload the file in ASCII mode, and simply direct your browser to display.php?id=123 to see 123.gif displayed where you put that code!
 
and at the bottom i want a link to the next picture and the previous picture....and thanks for all the help! :)
 
For the title thing, you have to do it this way:

Code:
<?php
echo "<title>Picture No. $id</title>";
?>

Now, for the links to the next and previous pictures, this can be done with a little simple subtraction and addition, but it will take a little more code.

Put the following code where ever you want the "Next Image" and "Previous Image" links to be generated.

Code:
<?php
// Determine Variables
$next_id = $id + 1;
$prev_id = $id - 1;

// Print Link(s)
echo "<a href=$php_self?id=$next_id>Next Image</a>;"
echo " | ";
echo "<a href=$php_self?id=$prev_id>Previous Image</a>;"
?>

You can obviously change the format of that--right now you would see something similar to this:

Next Image | Previous Image

* Notice the ?php_self variable, that saves lots of time--that variable always parses to the location of the PHP document, that way you don't have to retype/copy & paste long URLs all the time.

That should work fine, if it doesn't, I probably just made a stupid mistake... :)

[Edited by Niaad on 03-18-2001 at 10:28 PM]
 
i tried it but it says there is an error in line 21...can you help

Code:
<?php

// If no ID is specified, use a default.
if ($id == 0) {
	$id = 1;
}

// Print the image.
echo "<img src=C:\WINDOWS\Desktop\mysite\bspics\BSpics\$id.jpg>";

?>
<?php
// Determine Variables
$next_id = $id + 1;
$prev_id = $id - 1;

// Print Link(s)
echo "<a href=$php_self?id=$next_id>Next Image</a>;"
echo " | ";
echo "<a href=$php_self?id=$prev_id>Previous Image</a>;"
?>

please tell me the exact code i should save in the file

here it is
cgi-bin.spaceports.com/~bsvsca/display.php
 
This should do the same thing more safely and efficiently. Note that you probably don't want the img src to point to your desktop...


<?$id=empty($id)?1:floor($id)?>
<img src="C:\WINDOWS\Desktop\mysite\bspics\BSpics\<?=$id?>.jpg">
<a href="<?=$PHP_SELF?>?id=<?=$id+1?>">Next Image</a>
|
<a href="<?=$PHP_SELF?>?id=<?=$id-1?>">Previous Image</a>
 
i'll try it...ya i know i don't want it on my desktop but i haven't uploaded the images yet so i just want to try it out!...i'll tell you how it goes!
 
dude!!!!that worked perfect!!!!!!!!
i think i love you ;)

just one problem...when i put display.cgi?id=01 it shows 1.jpg and doesn't put in the 0 in front of the 1....can you please fix this bug?
 
php switches between types so it's treating 01 as the number '1' not the string '01' the easy way is not to have any files starting with a 0. you could make sure it was a string but then the next and previous buttons would f**k.



plus having a number means you can error trap and not try to load a picture if it's not in the range of your pics - but then you'd have to update the page when you added more pictures ...

- or you could get it to check the file exists on the server .....

[knowing when to stop is important - keep it simple]
 
Originally posted by Woofcat
This should do the same thing more safely and efficiently. Note that you probably don't want the img src to point to your desktop...


<?$id=empty($id)?1:floor($id)?>
<img src="C:\WINDOWS\Desktop\mysite\bspics\BSpics\<?=$id?>.jpg">
<a href="<?=$PHP_SELF?>?id=<?=$id+1?>">Next Image</a>
|
<a href="<?=$PHP_SELF?>?id=<?=$id-1?>">Previous Image</a>


...Just wondering (i'm a bit new to php)... what does this line do?:

$id=empty($id)?1:floor($id)

...ignore this post if its a dumb question ;)
 
I apologize, this was my fault...(stupid mistake).

In the last two lines, where the "Next Image" and "Previous Image" links are printed, the ; should have been outside the quotes, not inside.

Originally posted by coolguy23
i tried it but it says there is an error in line 21...can you help

Code:
<?php

// If no ID is specified, use a default.
if ($id == 0) {
	$id = 1;
}

// Print the image.
echo "<img src=C:\WINDOWS\Desktop\mysite\bspics\BSpics\$id.jpg>";

?>
<?php
// Determine Variables
$next_id = $id + 1;
$prev_id = $id - 1;

// Print Link(s)
echo "<a href=$php_self?id=$next_id>Next Image</a>";
echo " | ";
echo "<a href=$php_self?id=$prev_id>Previous Image</a>";
?>

please tell me the exact code i should save in the file

here it is
cgi-bin.spaceports.com/~bsvsca/display.php
 

...Just wondering (i'm a bit new to php)... what does this line do?:

$id=empty($id)?1:floor($id)

...ignore this post if its a dumb question ;)

funny little expression

$var = (condition) ? (value1) : (value2) ;

if condition is true it equates to value1 else to value2

same as

if (condition) {$var = value1}
else {$var= value2}

it's great for making your code look complicated
can be v useful
 
A variable variable is something complety different.

Lets say you had a variable named pizza which equals cheese:

$pizza = "cheese";

You could use the text in the pizza variable and make it into a variable:

$$pizza

Resulting in a variable named cheese ($cheese).

That's a variable variable. =)
 
Originally posted by BillWill
A variable variable is something complety different.

Lets say you had a variable named pizza which equals cheese:

$pizza = "cheese";

You could use the text in the pizza variable and make it into a variable:

$$pizza

Resulting in a variable named cheese ($cheese).

That's a variable variable. =)
Using a variable's contents as a variable's name is a bad idea for many reasons. Read Mark-Jason Dominus's articles at http://perl.plover.com/varvarname.html for more information. They're on the topic of Perl, but PHP has the same problem AFAIK. A good quote: "The real problem is that $$fields = ... is not confined at all, and if something goes wrong, it can smash any variable in the entire program, which will have a bizarre effect, possibly on something far away. "
 
Not to mention it confuses the hell out of newbie programmers and people who just don't know much about that feature.
 
Back
Top