Jump to content

getting a variable from rows


matttherat

Recommended Posts

Hi ive got this code which echos out all the rows from an SQL table.

 

while($row = mysql_fetch_array($result))

  {

  echo "<tr>";

  echo "<td>" . $row['subject1'] . "</td>";

  echo "<td>" . $row['subject2'] . "</td>";

  echo "<td>" . $row['subject3'] . "</td>";

  echo "<td>" . $row['subject4'] . "</td>";

  echo "<td>" . $row['email'] . "</td>";

  echo "</tr>";

  }

echo "</table>";

 

 

All i want to do is add all the 4 subject together and echo it out. simple but not sure how to do it. any ideas please?

 

Thanks Matt

Link to comment
Share on other sites

Edit: Basically what they said ^^^

 

Could you clarify by providing an example of what you mean by 'add all the 4 subject together'?

 

What you posted could mean any of -

 

1) Add all the same name field values together,

2) Concatenate all the same name field content together,

3) Add up the 'subject1', 'subject2', 'subject3', and 'subject4' field values for each row or for all the rows,

4) Concatenate the 'subject1', 'subject2', 'subject3', and 'subject4' field content for each row or for all the rows.

 

Also, how does your title 'getting a variable from rows' relate to your question?

Link to comment
Share on other sites

Sorry I wasn't clear. The 4 rows being echoed subject 1, subject 2, 3 and 4 are all integers. I want to add these 4 rows together and echo out the total of the 4 fields combined. the full code is here.

 

 

<?php

 

 

$con = mysql_connect("localhost","","");

 

 

 

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("mainbase", $con);

 

$result = mysql_query("SELECT * FROM agegame");

 

echo "<table border='1'>

<tr>

<th>subject1</th>

<th>subject2</th>

<th>subject3</th>

<th>subject4</th>

<th>email</th>

</tr>";

 

while($row = mysql_fetch_array($result))

  {

  echo "<tr>";

  echo "<td>" . $row['subject1'] . "</td>";

  echo "<td>" . $row['subject2'] . "</td>";

  echo "<td>" . $row['subject3'] . "</td>";

  echo "<td>" . $row['subject4'] . "</td>";

  echo "<td>" . $row['email'] . "</td>";

  echo "</tr>";

  }

echo "</table>";

?>

Link to comment
Share on other sites

To add the four columns together, you'd want to do something like this:

 

<?php

while($row = mysql_fetch_array($result))
{
  echo "<tr>";
  echo "<td>" . ($row['subject1'] + $row['subject2'] + $row['subject3'] + $row['subject4']) . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "</tr>";
}

Link to comment
Share on other sites

You should not be learning php, developing php code, or debugging php code on a live server. You waste a ton of time, both in constantly uploading changes just to see one result and in problems like the error_reporting/display_errors settings.

 

You likely introduced a fatal parse error and you would need to set the error_reporting/display_errors settings in a local php.ini (when php is running as a CGI) or in a .htaccess file (when php is running as an Apache module.)

 

You could post your current code so someone could determine what was wrong with it.

 

You would be better off doing this on a local development system.

Link to comment
Share on other sites

ah ha. its working. i left a echo in that wasnt formatted properly.  the only problem now is it isnt outputting the answers in new rows each time. is reading 103138126 etc when should be 103 138 126 with each answer inline with each row. eg

 

sub1  sub2    sub3    sub4      total

25 +    25 +    25 +      25 =      100

 

 

Link to comment
Share on other sites

Does this produce a blank page?

<?php
while($row = mysql_fetch_array($result))
{
  $sum = $row['subject1'] + $row['subject2'] + $row['subject3'] + $row['subject4'];
  echo 'Test sum: ' . $sum;
  exit;
}

 

Just try simple debugging, until you narrow it down... maybe you need to caste the subjects into ints. You may want to consider setting up a development environment on your local computer, so you can turn errors on and have complete control over everything. Once it's working perfectly, upload it to your hosting provider.

Link to comment
Share on other sites

YAY got it working. thank u everybody for your help. the final code was this.

 

echo "<tr>";

  echo "<td>" . $row['subject1'] . "</td>";

  echo "<td>" . $row['subject2'] . "</td>";

  echo "<td>" . $row['subject3'] . "</td>";

  echo "<td>" . $row['subject4'] . "</td>";

  echo "<td>" . $row['email'] . "</td>";

  $sum = $row['subject1'] + $row['subject2'] + $row['subject3'] + $row['subject4'];

  echo "<td>" . $sum . "</td>";

  echo "</tr>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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