takn25 Posted March 10, 2011 Share Posted March 10, 2011 Hi, I am fairly new to PHP but trying to pick it up as I go and Phpfreaks has been a great help thanks everyone much appreciated. The problem I am facing is I want to call a class which should have a while Loop I tried a few ways but no luck so I am just requesting if some one could post me an example how can I achieve this. Greatly Appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/230220-while-loop-in-a-class/ Share on other sites More sharing options...
onlyican Posted March 10, 2011 Share Posted March 10, 2011 I am confused, a class is usually a cluster of functions relating to the same thing. For example, I just wrote an upload class, which handles uploading / checking / manipulating the image That gets called from another class where the form is uploaded (As I work in OOP, everything is classes) So basically to answer your question class myClassName{ private $arrSomething = array(); public function __construct($arrSomething){ $this->setArrSomething($arrSomething); } //------------------------ private function setArrSomething($arrSomething){ if(is_array($arrSomething){ $this->arrSomething = $arrSomething; } } //------------------------ private function doingSomethingWhile(){ while($this->arrSomething => $strSomething){ //I am doing something i think. Not Sure. } } Quote Link to comment https://forums.phpfreaks.com/topic/230220-while-loop-in-a-class/#findComment-1185596 Share on other sites More sharing options...
takn25 Posted March 10, 2011 Author Share Posted March 10, 2011 The reason for this is because I was hard coding every while loop etc with HTML and in the end the page became barely readable for myself. Some one suggested that they use Class and call it isnt this the proper way? If not could you guide me what should I do to achieve this thanks! Quote Link to comment https://forums.phpfreaks.com/topic/230220-while-loop-in-a-class/#findComment-1185619 Share on other sites More sharing options...
Infected.Shadow Posted March 10, 2011 Share Posted March 10, 2011 The reason for this is because I was hard coding every while loop etc with HTML and in the end the page became barely readable for myself. Some one suggested that they use Class and call it isnt this the proper way? If not could you guide me what should I do to achieve this thanks! It's a tad hard to understand what you are trying to accomplish. Perhaps posting the code you've been working with could help make it clearer what you are trying to accomplish. From what I can gather: It sounds like you're trying to reduce the amount of html used in the php file, and some one suggested you use a class. They might have been referring to a template class. If that is the case then I personally suggest following tutorial: http://www.pixel2life.com/publish/tutorials/457/simple_template_engine_find_and_replace_variables/ Quote Link to comment https://forums.phpfreaks.com/topic/230220-while-loop-in-a-class/#findComment-1185727 Share on other sites More sharing options...
takn25 Posted March 11, 2011 Author Share Posted March 11, 2011 Hi thanks for helping I guess I was not clear enough. This is some part of the code, as its very long and waste of time for you lot to read it all. <div id="rGrid"> <div id="con"> <h3>cont <?php echo "(".$c.")"; ?></h3> </div> <div id="cont3"> <?php // echo the contents. while ($row4 = mysql_fetch_assoc($result)){ $data=$row4['data']; $tiny=$row4['tiny']; $id=$row4['id']; echo " <div class='p_1'> <div class='p_2'><img src='tiny'></img></div> <div class='p_3'><a href='soon.php?id=$data'>$fri</a> </div> </div> "; } ?> </div> </div> Ok the part I am talking about and thinking of taking to an external file is the echo all contents. The reason for this is I have about 7-8 While loops on the same PHP file with the HTML side by side and making it very difficult for me the point out where I am . So taking in from peoples suggestion I have learnt to make things neater it is best to use a class. They suggested its best to Include a single line of code for example Include ("class-file-for-loop") and run the while loop in an external file. If I am heading in the wrong direction please point out and suggest what can I do to make things cleaner. Quote Link to comment https://forums.phpfreaks.com/topic/230220-while-loop-in-a-class/#findComment-1186034 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.