Jump to content

[SOLVED] php problem in storing array


richrock

Recommended Posts

Hi,

 

I've been using a scriptaculous library to process a list of items, and assign the list to an order.  This means that items added to a DB are given an item order that is not defined by item id.

 

However, I have a small problem.  The array starts the ordering as 0, and I need it to start at 1.  Any ideas how I can do this?

 

This is how the items are listed:

 

$number = mysql_num_rows($result);

 	for($x=1;$x<=$number;$x++){

		$row = mysql_fetch_array($result);
		$real_lot = $row['lot_order'];
            $real_id = $row['id'];
		$list .= "<li id=\"item_$row[id]\" class=\"catalogue_rows\" >";
            $list .= "<table border='0'><tr>";
            $list .= "<td width='50'>$real_lot</td>";
            $list .= "<td width='400'><strong>$row[title]</strong></td>";
            $list .= "<td width='100'>£$row[est_low] - $row[est_high]</td>";
            $list .= "<td width='150'>$row[firstname] $row[lastname]</td>";
            $list .= "<td>$row[lotrecnum] - $row[reclinenum]</td>";
            $list .= "</tr></table>";
            $list .= "<input type='hidden' name='lot_order[]' value='$x' />";
            $list .= "</li>\n";

 

And storing the items via ajax :

 

foreach ($_POST[sortlist] as $varname => $varvalue) {
$sql = "update jos_bid_auctions set lot_order = ".mysql_real_escape_string($varname)." where id = ".mysql_real_escape_string($varvalue);
    $result = mysql_query($sql) or die(mysql_error());
}

Link to comment
Share on other sites

Hi,

 

I tried it, and it still reorders starting at 0.  I also changed $real_lot to $row[lot_order] as seen below, but still no juice.  Was up till about 4am last night trying options on this....  >:(

 

$number = mysql_num_rows($result);

 	for($x=0; $x < $number; $x++){

		$row = mysql_fetch_array($result);
		$real_lot = $row['lot_order'];
            $real_id = $row['id'];
		$list .= "<li id=\"item_$row[id]\" class=\"catalogue_rows\" >";
            $list .= "<table border='0'><tr>";
            $list .= "<td width='50'>$row[lot_order]</td>";
            $list .= "<td width='400'><strong>$row[title]</strong></td>";
            $list .= "<td width='100'>£$row[est_low] - $row[est_high]</td>";
            $list .= "<td width='150'>$row[firstname] $row[lastname]</td>";
            $list .= "<td>$row[lotrecnum] - $row[reclinenum]</td>";
            $list .= "</tr></table>";
            $list .= "<input type='hidden' name='lot_order[]' value='$x' />";
            $list .= "</li>\n";

	}

Link to comment
Share on other sites

ok I don't quite understand you coz now you've confused me..

 

 

you want your RESULTS that you OUTPUT to start counting itself

 

E.G.

 

item_1

item_2

 

starting from 1?

 

can you link me to the page that this is happening on.. and tell me what should be different, because I'm at a loss right now..

Link to comment
Share on other sites

Okay, I'll try to explain.  Unfortunately you can't see it, it's a private system and not gone live...

 

The JS I've used allows me to list a bunch of items, and drag 'n' drop them, updating each change via Ajax, and a sort.php file, which is basically

 

foreach ($_POST[sortlist] as $varname => $varvalue) {
   $sql = "update jos_bid_auctions set lot_order = ".mysql_real_escape_string($varname)." where id = ".mysql_real_escape_string($varvalue);
    $result = mysql_query($sql) or die(mysql_error());
}

 

When the page is reloaded in the browser, it shows the new ordering...  I've attached a screenshot to show how the lot order looks AFTER reloading the page.  And I just need it to show 1,2,3, instead of 0,1,2,...

 

Hope this clears it up a bit more...

 

[attachment deleted by admin]

Link to comment
Share on other sites

Hahahaaaa!!!!  ;D ;D ;D ;D

 

Thanks RussellReal - pointed in the right direction.  Doing what you suggested gave me order numbers like (0+1), (1+2), etc...

 

I figured to make that into another string - so I wrote:

 

$row_count = $row['order'] + 1;

 

and added it before generating the list, and presto!  It works!  Thanks so much guys, I'll sleep tonight!  ;D ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.