Jump to content

Help where to put the coding please?


joemal

Recommended Posts

Hi guys,

 

I am needing some help of how to place the coding as I can not get it to work. 

 

--

 

Basically I am building a website for the local motorclub and want some code that will "add up" each persons results.

 

Here is what I have come up with but it does not work;

<?php $total = mysql_fetch_array("SELECT cat1 + cat2 + cat3 + cat4 + cat5 + cat6 AS total FROM results") ?>
					  <?php
$cautotest_results = mysql_query("SELECT * FROM results WHERE cat ='1' ORDER BY cattotal DESC");
if(mysql_num_rows($cautotest_results) == '') { echo "<p>No Results Available."; } else {


echo "<table width=\"800\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"greywritinglight\" align=\"center\">
<tr align=\"center\">
<td>Competitor</td>
<td>Vehicle</td>
<td>Class</td>
<td>Skipton</td>
<td>Weston</td>
<td>King Bros</td>
<td>Normans</td>
<td>Norwood</td>
<td>Uniroyal</td>
<td>Total</td>
</tr>";

$x=1; 
while($results_row = mysql_fetch_array($cautotest_results))
{
			if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif;
echo "<tr align=\"center\" bgcolor=\"" .$rowbgcolor. "\">";
echo "<td>" . $results_row['competitor'] . "</td>";
echo "<td>" . $results_row['catvehicle'] .  "</td>";
echo "<td>" . $results_row['catclass'] . "</td>";
echo "<td>" . $results_row['cat1'] . "</td>";
echo "<td>" . $results_row['cat2'] . "</td>";
echo "<td>" . $results_row['cat3'] . "</td>";
echo "<td>" . $results_row['cat4'] . "</td>";
echo "<td>" . $results_row['cat5'] . "</td>";
echo "<td>" . $results_row['cat6'] . "</td>";
echo "<td>" . $total . "</td>";
echo "</tr>";
$x++;
}
echo "</table>";
}
?>

Its the top line of coding... and the echo .total. at the bottom.

 

 

If you could please correct me I would be ever so grateful.

 

If you need more info please just ask. All help is appreciated.

 

JTM. 

Edited by JTM
Link to comment
Share on other sites

					  <?php
$cautotest_results = mysql_query("SELECT * FROM results WHERE cat ='1' ORDER BY cattotal DESC");
$total = mysql_fetch_array("SELECT cat1 + cat2 + cat3 + cat4 + cat5 + cat6 AS total FROM results");
if(mysql_num_rows($cautotest_results) == '') { echo "<p>No Results Available."; } else {


echo "<table width=\"800\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"greywritinglight\" align=\"center\">
<tr align=\"center\">
<td>Competitor</td>
<td>Vehicle</td>
<td>Class</td>
<td>Skipton</td>
<td>Weston</td>
<td>King Bros</td>
<td>Normans</td>
<td>Norwood</td>
<td>Uniroyal</td>
<td>Total</td>
</tr>";

$x=1; 
while($results_row = mysql_fetch_array($cautotest_results))
{
			if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif;
echo "<tr align=\"center\" bgcolor=\"" .$rowbgcolor. "\">";
echo "<td>" . $results_row['competitor'] . "</td>";
echo "<td>" . $results_row['catvehicle'] .  "</td>";
echo "<td>" . $results_row['catclass'] . "</td>";
echo "<td>" . $results_row['cat1'] . "</td>";
echo "<td>" . $results_row['cat2'] . "</td>";
echo "<td>" . $results_row['cat3'] . "</td>";
echo "<td>" . $results_row['cat4'] . "</td>";
echo "<td>" . $results_row['cat5'] . "</td>";
echo "<td>" . $results_row['cat6'] . "</td>";
echo "<td>" . $total . "</td>";
echo "</tr>";
$x++;
}
echo "</table>";
}
?>

So its like this?

 

then on the page it gets:

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in /home/jet2vi/public_html/download/modules/cautotest.php on line 11

Link to comment
Share on other sites

You really need to read before writing code.  You are doing your querying all backwards.

 

The best approach (IMHO):

$sql = "my query statement";
$qresults = mysqli_query($link,$sql);
while ($row = $qresults->fetch_array()
{
   (handle the contents of this result row)
}

You have shown two examples of your misunderstanding where you try and process data that you never queried.

First you write, second you execute, third you play with the results.

Edited by ginerjm
Link to comment
Share on other sites

I didnt really understand what to do with what you put. 

 

I have got the manual yes, not read it yet though.

 

Im new to coding and this is the last bit I need for the webpage. It worked perfectly as I used to "store" the total value on the database. But now I am trying to learn new ways of doing things.

 

thats why my coding may seem back to front to you.

 

so with your code do you mean;

<?php
$sql = "SELECT * FROM results WHERE cat1='1'";
$qresults = mysqli_query($link,$sql);
while ($row = $qresults->fetch_array()
if(mysql_num_rows($qresults) == '') { echo "<p>No Results Available."; } else {


echo "<table width=\"800\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"greywritinglight\" align=\"center\">
<tr align=\"center\">
<td>Competitor</td>
<td>Vehicle</td>
<td>Class</td>
<td>Skipton</td>
<td>Weston</td>
<td>King Bros</td>
<td>Normans</td>
<td>Norwood</td>
<td>Uniroyal</td>
<td>Total</td>
</tr>";

$x=1; 
while($results_row = mysql_fetch_array($qresults))
{
			if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif;
echo "<tr align=\"center\" bgcolor=\"" .$rowbgcolor. "\">";
echo "<td>" . $results_row['competitor'] . "</td>";
echo "<td>" . $results_row['catvehicle'] .  "</td>";
echo "<td>" . $results_row['catclass'] . "</td>";
echo "<td>" . $results_row['cat1'] . "</td>";
echo "<td>" . $results_row['cat2'] . "</td>";
echo "<td>" . $results_row['cat3'] . "</td>";
echo "<td>" . $results_row['cat4'] . "</td>";
echo "<td>" . $results_row['cat5'] . "</td>";
echo "<td>" . $results_row['cat6'] . "</td>";
echo "<td>" . $total . "</td>";
echo "</tr>";
$x++;
}
echo "</table>";
}
?>
Link to comment
Share on other sites

You should really read the manual for the functions you are using.  There are many good examples of how they relate to other functions so it will help you construct some good code.

 

1 - your current code cannot possibly run - Turn On Error Checking at the top of every php script you write while in development mode.

 

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');

 

2 - Plan what you want to do - your current process is not in the correct order to do your task.  You do a fetch of a query result (with an error that makes the statement non-executable) and THEN you check to see how may rows were returned.  Does anything seem backwards here?

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.