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
https://forums.phpfreaks.com/topic/140935-solved-php-problem-in-storing-array/
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";

	}

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

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]

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.