PDA

View Full Version : anyone know php?



coolguy23
March 17th, 2001, 21:22
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!!!

Niaad
March 17th, 2001, 22:12
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:



<?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.)



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


Easy :)

coolguy23
March 17th, 2001, 22:30
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!! :)

Niaad
March 18th, 2001, 00:19
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.



<?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!

coolguy23
March 18th, 2001, 06:48
thanks alot!!!!
i'll try it and if i need more help i'll come back!

coolguy23
March 18th, 2001, 09:08
and one more thing...how would i put Picture No. "the id"
would it be
<title>Picture No. $id</title>

coolguy23
March 18th, 2001, 09:13
and at the bottom i want a link to the next picture and the previous picture....and thanks for all the help! :)

Niaad
March 18th, 2001, 12:41
For the title thing, you have to do it this way:



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



<?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 (http://url_of_your_php_document?id=1) | Previous Image (http://url_of_your_php_document?id=3)

* 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]

coolguy23
March 30th, 2001, 12:10
i tried it but it says there is an error in line 21...can you help


<?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

Woofcat
March 30th, 2001, 13:29
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>

coolguy23
March 30th, 2001, 16:32
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!

coolguy23
March 30th, 2001, 16:38
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?

lucifer
March 30th, 2001, 17:31
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]

TheSpaceDude
March 30th, 2001, 22:30
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 ;)

Niaad
March 30th, 2001, 23:01
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


<?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

lucifer
March 31st, 2001, 05:20
...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

Koolguy
March 31st, 2001, 15:39
Wait a sec, thats variable variable right?

BillWill
March 31st, 2001, 16:23
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. =)

Xyzzy
March 31st, 2001, 16:50
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. "

Koolguy
March 31st, 2001, 17:50
Not to mention it confuses the hell out of newbie programmers and people who just don't know much about that feature.

lucifer
April 1st, 2001, 10:18
but it's reasuring to know that you can do these things. The language gives you the freedom to write dangerous random almost impossible to debug code.

the choice is yours but do it at your peril