PDA

View Full Version : My first attempt at a PHP Script


Tristan Gray
08-12-2005, 04:13 PM
Some guy was looking on the graphics forum for a way to update stock levels graphically on his website. Basically he wanted a graphic to display how much out of 200 stock there remained in a sort of meter look. I have never done much with PHP but used to program java (not javascript) way back in the day. So anyway, here is my first stab at a PHP script please tell me if this would even run. I can't test because our local http server does not even support PHP (lame.)

// This Script was created by Tristan Gray to update visually stock levels from a database
// Put this in the html document where you want the script to load data <?php include "stock.php"; ?>


mysql_connect("localhost","username","password") or print "Unable to connect to MySQL!";

mysql_select_db("database_name") or print "Unable to get our database!";

$sql="your SQL query here";

$result = mysql_query($sql);

mysql_free_result ($result);

mysql_close ();

if ($result == 200)
{
$image == "8";
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"
}
elseif ($result < 200 && $result > 150)
{
$image == "7";
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"
}
elseif ($result == 150)
{
$image == "6";
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"
}
elseif ($result < 150 && $result > 100)
{
$image == "5";
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"
}
elseif ($result == 100)
{
$image == "4";
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"
}
elseif ($result < 100 && $result > 50)
{
$image == "3";
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"
}
elseif ($result == 50)
{
$image == "2";
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"
}
elseif ($result < 50 && $result > 0)
{
$image == "1";
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"
}
elseif ($result == 0)
{
$image == "0";
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"
}
else
{
$image == "9.gif";
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";
}

$file_type = ".gif";
$image_folder = "images/stock";
$image_name = $image . $file_type;

Anyways, any feed back would be appreciated as I am certain there is a more efficient way to do this. Maybe switches or something. But I'd really like to see if this would even run. hehe. Thanks.

Tristan Gray
08-12-2005, 06:00 PM
Any suggestions are helpful, seriously.

Anthony2oo4
08-12-2005, 10:20 PM
im not good with php ethier, but it looks good to me. Only thing it, it would have to be refreshed all the time for a bar to display (i think) Because PHP is server side, therefore it would be getting updated on the server.

firepages
08-13-2005, 03:24 AM
You should declare your variables before you try to use them...
e.g $image_folder is empty until after you use it.


$result = mysql_query($sql);
....
if ($result == 200)

$result is a resource not a value (and will equate as something like '#Resource ID1') , to get values from the resource you need to use mysql_fetch_row/array/assoc/object (http://www.php.net/mysql_fetch_row).

for a large set of conditionals a switch() would probably be preferable to multiple if..else 's though thats not really an issue , not terminating php statements with a semi-colon however is and will cause parse errors...

echo "blah" //error
echo "blah" ; //no error

Good on you for having a go but really you cant write scripts if you dont have an environment to test them , peeps who write PHP all day long still churn out parse errors so for someone new to php you need a test environment.

Fou-Lu
08-13-2005, 05:20 AM
As firepages says, you need an environment. Simple to install, and the greatest thing ever :p. Way back when I was first learning, I used my webhost for this as I didn't know that I could even install an environment on my pc... ah the good old days.
Lol.

Anyway, on with your script. Offhand, I'd say that it won't work for you.
As firepages also mentioned, I'd go with a switch as well, simply due to the fact that it will evaluate quicker than the if..else, at least I believe that it will. However, there is not a problem with using if...else if you are more comfortable with it.
This is what I'm looking at:
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";
each of your ifs include this code. However, none of them have a prior initialization to the variables $image_folder or $image_name, which you have declared later (as a image.gif.gif on image #9 mind you, you need to watch for that). Since you are directly sending this output and not combining the values at a later point, your output would be:
<img src="" alt="" />
Also mentioned you will need to include a mysql_fetch[row/array/assoc/object] function in order to make use of your $result variable -> which is currently only a resource.
Not mentioned yet is that your values are incorrectly assigned. == in php is a comparison operator, = is an assignment. You need to state variables as $var = $value, and evaluate them using ==.

Another option available to you is to make use of the GD library. I cannot tell you if its configured to be used on your environment or not, nor can I tell you what functions are available for you to use. However, the GD will allow you to conditionally create images as you need them to be, which may be quite handy.