rn14 Posted November 22, 2008 Share Posted November 22, 2008 Below is a class that I use to store inofrmation that is in an array. Below in the function display I printing this info. However i want to be able to print this data somewhere else how will I call the function to do this? Each value that is in the array must be created as a variable? Thanks for any help with this. <? class Order { var $TestArray = array(); function Test($inputArray) { $this->setTestArray($inputArray); $this->Display(); } function Display() { $array = $this->TestArray; $meal1 = $array[0]; echo $meal1."<br>"; $meal2 = $array[1]; echo $meal2."<br>"; $meal3 = $array[2]; echo $meal3."<br>"; $meal4 = $array[3]; echo $meal4."<br>"; $meal5 = $array[4]; echo $meal5."<br>"; $meal6 = $array[5]; echo $meal6."<br>"; $meal7 = $array[6]; echo $meal7."<br>"; foreach($this->TestArray as $value) { //echo $value. "<br>"; } } function setTestArray($inputArray) { if(is_array($inputArray)) { $this->TestArray = $inputArray; } } function echoArrayA1() { $a = array('a'); foreach($a as $value) { //echo "<br />" . $value; } } function echoArrayA2() { $a[0] = 'b'; foreach($a as $value) { // echo "<br />" . $value; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/133797-array/ Share on other sites More sharing options...
corbin Posted November 22, 2008 Share Posted November 22, 2008 function GetData() { return $this->TestArray; } function SetData($data) { $this->TestArray = $data; } Then you could use GetData to get and do what ever you want with the array somewhere else. No offense, but that's some pretty crappy OOP code. The syntax is fine, but the actual design of it is very... bad. Perhaps look into OOP more in design and practice? Quote Link to comment https://forums.phpfreaks.com/topic/133797-array/#findComment-696303 Share on other sites More sharing options...
rn14 Posted November 22, 2008 Author Share Posted November 22, 2008 im trying to learn oop at the moment could you explain the reasons for its poor design and how I can rectify them? When i use this $array = $order->getData(); to access getData how will I extract the the different values in the array? Thanks and apologies for the basic and stupid questions Quote Link to comment https://forums.phpfreaks.com/topic/133797-array/#findComment-696317 Share on other sites More sharing options...
rn14 Posted November 22, 2008 Author Share Posted November 22, 2008 could anybody help me with this??? Quote Link to comment https://forums.phpfreaks.com/topic/133797-array/#findComment-696336 Share on other sites More sharing options...
gevans Posted November 22, 2008 Share Posted November 22, 2008 foreach($array as $key => $value){ echo "$key: $value"; } Quote Link to comment https://forums.phpfreaks.com/topic/133797-array/#findComment-696342 Share on other sites More sharing options...
rn14 Posted November 22, 2008 Author Share Posted November 22, 2008 Thanks, can i let the value returned equal to a variable?? Quote Link to comment https://forums.phpfreaks.com/topic/133797-array/#findComment-696345 Share on other sites More sharing options...
gevans Posted November 22, 2008 Share Posted November 22, 2008 You can assign them all to a variable, but there isn't much need. If you already know what's in the array you can use the array; echo $array[0];//the first var echo "<br /><br />"; echo $array[1];//the second var Quote Link to comment https://forums.phpfreaks.com/topic/133797-array/#findComment-696354 Share on other sites More sharing options...
corbin Posted November 22, 2008 Share Posted November 22, 2008 im trying to learn oop at the moment could you explain the reasons for its poor design and how I can rectify them? When i use this $array = $order->getData(); to access getData how will I extract the the different values in the array? Thanks and apologies for the basic and stupid questions It wasn't a stupid question. I wasn't trying to call you or your questino stupid, I was just trying to say that you have the wrong approach. Quote Link to comment https://forums.phpfreaks.com/topic/133797-array/#findComment-696360 Share on other sites More sharing options...
rn14 Posted November 22, 2008 Author Share Posted November 22, 2008 Thanks, How do i prevent this error from happening when no data is present?? Fatal error: Call to a member function getData() on a non-object Quote Link to comment https://forums.phpfreaks.com/topic/133797-array/#findComment-696365 Share on other sites More sharing options...
rn14 Posted November 22, 2008 Author Share Posted November 22, 2008 I didnt think you were corbin. Could you help me to get the right approach with the code that i have? Quote Link to comment https://forums.phpfreaks.com/topic/133797-array/#findComment-696374 Share on other sites More sharing options...
corbin Posted November 22, 2008 Share Posted November 22, 2008 Fatal error: Call to a member function getData() on a non-object That simply means that the variable on which you are trying to call the getData method is not an object. "Could you help me to get the right approach with the code that i have?" Well the only thing really wrong with it is that I don't think it's the Order classes job to handle output. Shouldn't it be just handling orders, not displaying them? When the actual class handles displaying the data, you run into problems like that one you had. (Although in this case you could have just modified the function to only set new data if new data were provided.) Quote Link to comment https://forums.phpfreaks.com/topic/133797-array/#findComment-696417 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.