pouncer Posted April 10, 2007 Share Posted April 10, 2007 say for e.g i want to add an array mapping $ratings = array("1" => "Poor"); how do i add more mappings to $ratings like that? i want to add 2 => Good Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted April 10, 2007 Share Posted April 10, 2007 <?php $ratings[2] = "good"; $ratings[3] = "excellent"; // etc... ?> Maybe I misunderstood the question? Patrick Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 10, 2007 Author Share Posted April 10, 2007 thanks! what if the array was initially empty like $ratings = array(); can we still add mappings to it like u showed? Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted April 10, 2007 Share Posted April 10, 2007 Yup. Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 10, 2007 Author Share Posted April 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
per1os Posted April 10, 2007 Share Posted April 10, 2007 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]; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 10, 2007 Share Posted April 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 10, 2007 Author Share Posted April 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
per1os Posted April 10, 2007 Share Posted April 10, 2007 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. Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 10, 2007 Author Share Posted April 10, 2007 thanks buddy fore the reply and code. it's just just giving me a blank page on view_item_rating.php Quote Link to comment Share on other sites More sharing options...
per1os Posted April 10, 2007 Share Posted April 10, 2007 Maybe the code to view_item_rating.php would help? Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 10, 2007 Author Share Posted April 10, 2007 $test = $_POST['arr']; echo $test[5]; that's all it is Quote Link to comment Share on other sites More sharing options...
per1os Posted April 10, 2007 Share Posted April 10, 2007 $test = $_POST['arr']; print 'test values ' . print_r($test,true); Try that. Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 10, 2007 Author Share Posted April 10, 2007 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; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.