Jump to content

While loop in a class


takn25

Recommended Posts

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.

Link to comment
Share on other sites

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.
}
}

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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  :confused:. 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. 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.