0

I need to add a counter to the loop as well as pulling contents. What is the syntax for adding the "and for ($ColCount = ColCount + 1)"

<?php $ColCount = 0?>
<!--get artwork thumbnails --> 
<?php
$dbname = 'pdartist2';
$table = 'artwork';
// query
$result = mysql_query("SELECT AID, ThumbFilePath, Title, DisplayOrder FROM artwork where SCID = '$SCID' ") or die(mysql_error());
while($row = mysql_fetch_row($result))
{
    foreach($row as $cell)
    and for ($ColCount = ColCount + 1) 

echo "$ColCount", "$cell";
}
mysql_free_result($result);
?>
3
  • 1
    Why not just use $ColCount++; Commented Oct 25, 2011 at 15:45
  • I think you actually mean row num, not col count, right? Commented Oct 25, 2011 at 15:48
  • 1
    @The old dog Seeing this questions and these others I suggest you read more on PHP syntax Commented Oct 25, 2011 at 15:51

6 Answers 6

2

I have no idea if that's valid in PHP. Maybe all it is is a forgotten $ sign before ColCount. But you could just do it the normal way:

foreach($row as $cell) {
    $ColCount++;

    echo $ColCount . $cell;
}

$ColCount = 0;
Sign up to request clarification or add additional context in comments.

2 Comments

nope your right, the code in the question is very odd. Not sure what is wanted but was going to post the same solution.
I posted the same solution, except with $ColCount = 0; in the right place, and got a downvote without comments :(
1

Code:

<!--get artwork thumbnails -->      
<?php  
$dbname = 'pdartist2';  
$table = 'artwork';  
// query  
$result = mysql_query("SELECT AID, ThumbFilePath, Title, DisplayOrder FROM artwork   where SCID = '$SCID' ") or die(mysql_error());  
while($row = mysql_fetch_row($result))  
{  
    $colCount = 0;  
    foreach($row as $cell) {  
      $colCount++;  
      echo "$ColCount", "$cell";  
    }  
}  
 mysql_free_result($result);  

?>

1 Comment

This place is awesome! How would I add if ColCount is 1 <div id="clear"></div>
0

As Bruce said, you could just do $ColCount++, But also you could just count the results with mysql_num_rows

$result = mysql_query("SELECT AID, ThumbFilePath, Title, DisplayOrder FROM artwork where SCID = '$SCID' ") or die(mysql_error());
$numRows = mysql_num_rows($result);

Shai

1 Comment

That will provide the number of rows in the result set, not a column count for each row.
0

You are missing a semi-colon:

<?php $ColCount = 0?>

Should be:

<?php $ColCount = 0; ?>

And:

while($row = mysql_fetch_row($result)) {
    foreach($row as $cell) {
        $ColCount++; // adds 1 to colcount

        echo $ColCount.$cell; // no need for quotes around variables. Also string concatination in PHP is using s dot (.)
    }
 }

mysql_free_result($result);

4 Comments

The column count has to be reset for each row. Doing it once on top of the script will just count the total number cells in the result set.
@ÁlvaroG.Vicario: as you can see I don't only use the total count, but I also print the colcount in the loop. That's what OP wants if I'm correct. I might be wrong though. Bit hard to tell what OP actually wants to achieve... :)
Yes, but you don't set it back to zero when you start printing a new row.
@ÁlvaroG.Vicario: I don't know if that is what the OP wants. He only states he needs a counter.
0

What i'll do is use an array like that

    <!--get artwork thumbnails --> 
    <?php
    $dbname = 'pdartist2';
    $table = 'artwork';

    # Define array
    $Count = array();

    // query
    $result = mysql_query('SELECT AID, ThumbFilePath, Title, DisplayOrder FROM artwork where SCID = '.$SCID) or die(mysql_error());
    while($row = mysql_fetch_row($result)){
        foreach($row as $cell){
            $Count[$Cell]['Count'] = $Count[$Cell]['Count']++;
        }

        echo $ColCount.', '.$cell;
    }
    mysql_free_result($result);

So you have the count for each colomn but what I suggest is to use the mysql_num_rows(); like that :

$Query = mysql_query... $Count = mysql_num_rows($Query);

$Count will be a number ;)

Then if($Count > 0)

Comments

0

If you're trying to count the number of columns in each row, then

while($row = mysql_fetch_row($result)) {
   $colCount += count($row);
}

though I fail to see point of this. an SQL result set is a perfectly rectangular array. Variable number of rows, but every row will ALWAYS have the same number of fields as all the other rows in the result set. You'd only have to count the columns ONCE and cache that value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.