Help! Major array issues...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gdrums
Forum Newbie
Posts: 7
Joined: Mon Nov 18, 2002 4:41 pm

Help! Major array issues...

Post by gdrums »

So I've been trying to get this array working for a few days and I'm not having any luck at all. I'm a cold fusion programmer discovering PHP and I haven't been able to find a good resource on arrays for PHP. Does anyone have any good sources. The php.net explnations seem to be a little to cryptic for me.

The code I'm using is below if anyone has any other ideas. Im trying to load a sort order variable into an array so i can loop through the update statement when new values have been selected from the sort drop downs...

Code: Select all

<?php if ($update == "true") echo print_r($sortlist);
//array loop will go here and update the database once i figure it out
//mysql_query("UPDATE PAGE_CONTENT SET
// SORT = '$sort'
// WHERE CONTENT_ID=$content_id")
// or die ('could not update');
?>

<form action="index.php?fuseaction=admin_company.sort&page_id=<?php echo "$page_id";?>&update=true" method="post">
		
<?php
  //grab the content and loop through order by the sort column
		$Result = mysql_query("SELECT * FROM PAGE_CONTENT WHERE PAGE_ID=$page_id order by SORT");
		while ($Row = mysql_fetch_row($Result))
		
		//output the content
		&#123;
		 if (strlen($Row&#1111;2]) > 0 ) echo "<h1>$Row&#1111;2]</h1>";
		 if (strlen($Row&#1111;3]) > 0 ) echo "<h2>$Row&#1111;3]</h2>";
		 if (strlen($Row&#1111;4]) > 0 ) echo "<p>$Row&#1111;4]</p>";
		 if (strlen($Row&#1111;5]) > 0 ) echo"<img src="uploads/$Row&#1111;5]" border=0 alt="Row&#1111;8]"></a><br><br>";
		 if ($Row&#1111;6] <> "") echo "<a href="$Row&#1111;6]" target="_new"><font class=boxtext>more >></font></a><br>";
		 if ($Row&#1111;7] <> "") echo "<a mailto="$Row&#1111;7]"><font class=boxtext>$Row&#1111;7]</font></a><br>";
		 echo"<input type="hidden" name="content_id" value="$Row&#1111;0]">";	
		 $sort=$Row&#1111;14];
		 $mylist=$Row&#1111;0];
		 
		//load the sort order drop down 
		 echo "<font class=data>sorted as:</font>
				<select name="sort">";
				$Result2 = mysql_query("SELECT SORT FROM PAGE_CONTENT WHERE PAGE_ID=$page_id");
				while ($Row = mysql_fetch_row($Result2))
				&#123;
				print"<option  value="$Row&#1111;0]" "; if ($Row&#1111;0] == $sort) echo "selected"; echo">$Row&#1111;0]</option>";
				&#125;
			echo"</select> <br><br>";

		//load the array to pass to the update statement	
			$sortlist&#1111;]= array($sort);
		//output the array for testing purposes as it is being loadded
			echo print_r($sortlist);
		&#125;

		//load the hidden field with array values
		echo "<input type="hidden" name="real_list" value="";echo $sortlist; echo"">";
		?>
I hope that makes sense...thanks
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

What array are you talking about? The mysql_fetch_row one?

How are you passing the variables to the update portion of the script? If you are using the form, you should be aware that $row[] will not automatically pass to the next page. You have to define the variables with input types and then access them with $_GET or $_POST.

I have sympathy, seeing as I came from ColdFusion myself, but we need a bit more information as to what you are trying to do
gdrums
Forum Newbie
Posts: 7
Joined: Mon Nov 18, 2002 4:41 pm

Post by gdrums »

Yeah sorry, I didn't specify. MOstly i was looking for a good dev resourse for array's in PHP other the php.net, but thanks for trying to help.

The array I'm trying to load is the sortlist[] array below...

Code: Select all

//load the sort order drop down 
		 echo "<font class=data>sorted as:</font>
				<select name="sort">";
				$Result2 = mysql_query("SELECT SORT FROM PAGE_CONTENT WHERE PAGE_ID=$page_id");
				while ($Row = mysql_fetch_row($Result2))
				&#123;
				print"<option  value="$Row&#1111;0]" "; if ($Row&#1111;0] == $sort) echo "selected"; echo">$Row&#1111;0]</option>";
				&#125;
			echo"</select> <br><br>";
		//load the array to pass to the update statement	
			$sortlist&#1111;]= array($sort);
		//output the array for testing purposes as it is being loadded
			echo print_r($sortlist);

//load the hidden field with array values
		echo "<input type="hidden" name="real_list" value="";echo $sortlist; echo"">";
It's being past through the hidden field above. The problem I am having mostly is just in loading this arrary, it seems to work somewhat but the output of the array that i echo with the "print_r" doesn't look right. The form submits to itself with the update variable set to true, so it can then loop over that array in the update statement at the top.

Thanks
gdrums
Forum Newbie
Posts: 7
Joined: Mon Nov 18, 2002 4:41 pm

Array dev help

Post by gdrums »

Does anyone know of a good source for learning arrays in PHP other then http://www.php.net

I've looked thorugh that a few times and I just can't seem to figure it out, it might be over my head a little bit.

I just need to load a variable into an array or list from a while loop, then pass the values through a form, and then loop over the array with an update statement.

Any ideas?

thanks
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

I don't know of any resources, myself I find arrays with PHP to be very good in functionality and felxibility, especially cool (and sometimes frustrating) the mix of associative and indexed

I think you missunderstand slightly..

$var[] = 'something';

will add another value to the array $var

$var = array('something');

will make $var an array with one string element

so when you are doing

$var[] = array('someval');

You are creating an array with one string element and appending it to the $var array, meaning that if the indexposition happened to be 5 it would be true on
if ($var[5]['0] === 'someval') ...
gdrums
Forum Newbie
Posts: 7
Joined: Mon Nov 18, 2002 4:41 pm

Post by gdrums »

Thanks Stoker,

That cleared up the first issue. I now need to add a second varible on to the array though, so I tried an example i found on codewalkers.com

Code: Select all

$sortlist = array(0=>array('order'=$sort,'price'=>$content_id))
Unfortunatley this doesn't seem to work it error at the second = sign, as did the example code from codewalkers when i plugged it in. Is this close, I need to create and loop over this array and insert the sort id into the database where content_id=$content_id$ from the array. It's clearing up some but still a little muddled for me.

Thanks again.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

hmm you are missing a > in that assignment, any specific assignment in an array list must be done with =>

doing
$sortlist = array( 0 => array( 'order' => $sort, 'price' => $content_id ))

would be the same as
$sortlist = array( array( 'order' => $sort, 'price' => $content_id ))
(since your index id is 0)
and can be acessed like
$sortlist[0]['price']
$sortlist[0]['order']
Post Reply