Jump to content

array question - experts needed


pouncer

Recommended Posts

i gt this function

 

	function getArray($item_id) {
		$result = mysql_query("select count(*) AS tot_people_per_rating,rating FROM item_rating WHERE item_id='$item_id' GROUP BY rating");

		$list_all = array(1,2,3,4,5);
		$list_sel = array();

		$ratings = array(
				"1" => "Poor",
				"2" => "It's ok", 
				"3" => "Cool", 
				"4" => "Good", 
				"5" => "Excellent");

		$graph_data = array();

		$build_array_strings = "";

		while(list($tot_rating,$rating) = mysql_fetch_row($result)){
			$str = $rating . " => " . $tot_rating;
			$list_sel[] = $rating;

			$build_array_strings = $build_array_strings == "" ? $str : $build_array_strings . ", " . $str;

			$graph_data[$rating] = $tot_rating; 
		}

		$arr_diff = array_diff($list_all,$list_sel);

		for($i=1;$i<= count($arr_diff);$i++){
		   $str = $arr_diff[$i] . " => 0";
		   $build_array_strings = $build_array_strings == "" ? $str : $build_array_strings . ", " . $str;
		   
		   $graph_data[$arr_diff[$i]] = "0";
		}

		return $graph_data; 
	}

it returns the array perfectly cos i put echos in this class to test it etc.

 

but when i call the function like

 

$arr = $graph->getArray($item_id);

$test = array($arr);

echo "testtt" . $test[5];

 

it just echos testtt :s

Link to comment
Share on other sites

Try this:

 

echo "testtt" . $test[0][5];

 

The line $test = array($arr);

 

Creates a new array with the first value containing an array. You effectively created a multi-dimensional array.

 

To avoid the multi-dimm array do this:

 

$arr = $graph->getArray($item_id);
$test = $arr;
echo "testtt" . $test[5];

Link to comment
Share on other sites

You are making a two dimensional array with this code:

<?php
$arr = $graph->getArray($item_id);
$test = array($arr);
?>

do this instead:

<?php
$arr = $graph->getArray($item_id);
echo '<pre>' . print_r($arr,true) . '</pre>';
?>

This should dump to the screen what the array $arr contains.

 

Ken

Link to comment
Share on other sites

ok guys ive now got it to work but still a big problem

 

    <form name="form2" method="post" action="view_item_rating.php">
		      <div align="center" class="style74">
                   <input name="Submit2" type="submit" class="style74" value="View rating results!">
                   <?php
			   		$arr = $graph->getArray($item_id);
			   ?>
			   <input type='hidden' name='ar' value='<?php echo $arr; ?>'>
                   <br>
                </div>
		    </form>
<?php echo "tsting " . $arr[3]; ?> //THIS LINE ECHOS PERFECTLY!

 

im trying to send the array as a hidden value to view_item_rating.php

 

in  view_item_rating.php:

$test = $_POST['ar'];

echo $test[3];

it just echos nothing.. but a blank page :s

Link to comment
Share on other sites

You cannot just pass an array and expect it to work lol, try this.

 

                   <input name="Submit2" type="submit" class="style74" value="View rating results!">
                   <?php
			   		$arr = $graph->getArray($item_id);
                foreach ($arr as $value) {
                      print '<input type="hidden" name="arr[]" value="'.$value.'" />';
             }
			   ?>
                   <br>
                </div>
		    </form>
<?php echo "tsting " . $arr[3]; ?> //THIS LINE ECHOS PERFECTLY!

 

See what happens.

Link to comment
Share on other sites

i fixed it by just doing this

 

$arr = $graph->getArray($item_id);

 

for ($i = 0; $i <= 5; $i++) {

print '<input type="hidden" name="arr' . $i . '" value="'.$arr[$i].'" />';

}

 

 

 

then in the other file

 

$graph_data = array();

 

for ($i = 0; $i <= 5; $i++) {

$val = $_POST['arr' . $i];

 

$graph_data[$i] = $val;

}

 

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.