PHP!

Warning: annoying script ahead

Page 1 of 1

5 Replies - 1549 Views - Last Post: 31 May 2001 - 11:54 AM Rate Topic: -----

#1 Paradox  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 432
  • Joined: 03-March 01

PHP!

Posted 27 May 2001 - 11:38 AM

I'm creating a little script that puts data into two columns.  It alternates columns, so that the first piece of data goes in the top left column, then the second piece of data goes in the top right column, then the third piece of data goes in the left column of a new row, and so on.

Here's what I have:

$cols = 0;

for($i=0; $i < count($data); $i++){  if($cols = 0){    echo "<tr>";  }

 echo "<td width="50%">$data[$i]</td> ";  $cols++;

 if($cols > 1){    echo "</tr>";    $cols = 0;  } }

I know its something simple, what am I doing wrong?  Currently it just outputs the data in columns, never creating new rows.


Is This A Good Question/Topic? 0
  • +

Replies To: PHP!

#2 tsmoke  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 20-April 01

Re: PHP!

Posted 27 May 2001 - 02:46 PM

Looking at your code, I was wondering if you even needed the $cols variable. I have looked at what you have and I think that the following would work much easier.

for($i=0; $i < count($data); $i++){
   if($i%2 == 0){
     echo "<tr>";
   }
    echo "<td width="50%">$data[$i]</td>
";
 
   if($i%2 ==1){
     echo "</tr>
";
  }
}

Of course, you must have both openeing and closing table tags before and after executing this code or you will continue to get the same results.

tsmoke

Was This Post Helpful? 0
  • +
  • -

#3 Paradox  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 432
  • Joined: 03-March 01

Re: PHP!

Posted 27 May 2001 - 03:45 PM

It worked great! I haven't used the % operator before, but I just looked it up.  Looks useful

What if the data was an odd number such as 7.  You would then have 4 rows, but an empty space.  Is it possible to fill that in with something?

And I'd show everybody my results, but it's for my top secret new site ;)

Was This Post Helpful? 0
  • +
  • -

#4 tsmoke  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 20-April 01

Re: PHP!

Posted 29 May 2001 - 06:01 PM

Quote

What if the data was an odd number such as 7.  You would then have 4 rows, but an empty space.  Is it possible to fill that in with something?

I would start of by creating a variable that gets the number of values in your data array before starting the for loop and using it within the loop itself. Something similar to the following:

$num = count($data);

//Create odd variable $odd = FALSE; //If an odd number, then make it even if ($num % 2 == 1){    $odd = TRUE;    $num++; }

for($i=0; $i < $num; $i++){    if($i%2 == 0){      echo "<tr>";    }        if ($odd && ($i == ($num - 1))){        //Put the code to display your filler here.        echo "<td width="50%">$whatever here<td> ";    }else{        echo "<td width="50%">$data[$i]</td> ";    }                if($i%2 ==1){      echo "</tr> ";    } }

It takes just a little more code, but that should work for what you are trying to do.

tsmoke


Was This Post Helpful? 0
  • +
  • -

#5 megamu  Icon User is offline

  • Information Designer
  • member icon

Reputation: 0
  • View blog
  • Posts: 1,829
  • Joined: 13-May 01

Re: PHP!

Posted 31 May 2001 - 11:42 AM

just in case you were didnt fond out what you did wrong:
if($cols = 0){


needs to be changed to:
if($cols == 0){


---
the '=' operator defines a variable. For some unknown reason it doesnt matter most of the time, but if its in a for statement then you NEED to use '==' to test equality, because '=' just resets the variable.

I had this problem once, It took me about 5 hours to figure out what i did wrong, because i was so used to using '=' out of the for statement!

BLAH!
-eko-

Was This Post Helpful? 0
  • +
  • -

#6 Paradox  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 432
  • Joined: 03-March 01

Re: PHP!

Posted 31 May 2001 - 11:54 AM

LOL! I can't believe it was that simple, eko!!!
I know that == should've been used, but when I first looked for the problem, I didn't spot that.  And thanks tsmoke for the alternate script
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1