Optimise my PHP code

Soldato
Joined
15 Jan 2004
Posts
10,206
This code adds a category to the database categories with the fields c_id (auto), c_order, c_name, c_image

It works, but could probably be made more efficient.

I've used a URL for the code instead of BB tags because of the pretty colours :p

http://matstudios.com/temp/code.html

All criticism is welcome!

Thanks.
 
The random HTML is just the form which then returns the data back to the top of the page.

The code you are referring to checks if the order (of the category) entered by the user is lower than highest order number in the database.

And if so, all the rows above that users order have to moved up.

So If I have 4 categories, and I add another with the order '2', than the categories with order, 2, 3 & 4 have to move up (+1).
 
Yes, but the c_order will not longer be in use.

I think what I am trying to achieve and what you are interpreting are different.

The ID's are unique and only their for a reference point. The categories will shown in order by c_order.

Like this:

ID | Order
2 | 1
4 | 2
1 | 3
3 | 4

If I remove ID 4, where order = 3 needs to be moved down, same with the 4th order.

Is there another way to do it?
 
Yeah, after much thought though, I've decided to drop the row idea, and just order by name.

One more question:

Is this the most effective way of retrieving everything from a table? Row by row.

Code:
<?php require_once("../inc/core.php");
ConnectToDb($server, $username, $password, $database);
$sql = mysql_query("SELECT * FROM categories") or die (mysql_error());
$num_rows = mysql_num_rows($sql);

if ($num_rows > 0)
{
	$i=0;
	while ($i < $num_rows) {
	$id=mysql_result($sql,$i,"c_id");
	$name=mysql_result($sql,$i,"c_name");
	$image=mysql_result($sql,$i,"c_image");
	echo("$id $name $image <br/>");
	$i++;
	}
}
else
{
	echo("No categories created yet.");
}
mysql_close();
?>
 
Back
Top Bottom