PDA

View Full Version : Getting information from sql DB and display on Web Page



big-dog1965
12-24-2007, 04:48 AM
Is there a way to get the number of results from fields in Database
For instance if I have 4 fields say x1 x2 x3 and x4.
x1 has 3 records
x2 doesnt have any
x3 has 10 records
x4 20 records
On the web page it would show
x1 3
x3 10
x4 20
note x2 doesnt have any so nothing is shown but if it did then it would be shown like the others.
Also a row might have a record for a combination of x fields so the resulds would need to still be counted.

Medyman
12-24-2007, 03:35 PM
I'm not sure about your DB setup.

something like this will result in counting all the records in the entire table


SELECT COUNT(*) FROM table;

You might want to look more into the COUNT command for more info.
Also, have a look at this to see if it's more of what you want.

http://www.tizag.com/mysqlTutorial/mysqlcount.php

big-dog1965
12-24-2007, 05:33 PM
This is the start I think but its not real pacific see it at p.htm (http://www.ksproseries.com/p.htm)


$query = "SELECT Attend_Round, COUNT(Monster_Truck) FROM Entry_Form GROUP BY Attend_Round";
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo $row['Attend_Round'] ." ". $row['COUNT(Monster_Truck)'] ." items.";
echo "<br />";
}
?>
I think I need something more like this but dont know how to make it work. the above probably needs more to it as you can see I have more than just Monster truck to look at for the echo.

$result=mysql_query($query);

$num=mysql_numrows($result);

if ($num==0) {

echo "";
}else{
echo "";
}

mysql_close();

$i=0;
while ($i < $num) {

$Attend_Round=mysql_result($result,$i,"Attend_Round");
$Eighth_Scale_Buggy=mysql_result($result,$i,"Eighth_Scale_Buggy");
$Eighth_Scale_Sportsman=mysql_result($result,$i,"Eighth_Scale_Sportsman");
$Arena_Truck=mysql_result($result,$i,"Arena_Truck");
$Monster_Truck=mysql_result($result,$i,"Monster_Truck");
$Stadium_Truck=mysql_result($result,$i,"Stadium_Truck");
echo "<b>$Name<br>$Eighth_Scale_Buggy<br>$Eighth_Scale_Sportsman<br>$Arena_Truck<br>$Monster_Truck<br>$Stadium_Truck<hr>";

$i++;
}

big-dog1965
12-29-2007, 09:25 PM
Can someone please help me

Leafy
01-02-2008, 09:18 AM
I don't have much experience with SQL but I know you can do it with a (probably less resource-efficient) piece of code like this:



$query = "SELECT * FROM Entry_Form GROUP BY Attend_Round";
$result = mysql_query($query) or die(mysql_error());

// Print out result
$x = 1;
while($row = mysql_fetch_array($result)){
print "<b>Entry " . $x . "</b><br />";
$x++;
foreach($row as $key => $value) {
printf("%s %s<br />",$value,$key);
}
print "<hr />";
}
?>

big-dog1965
01-02-2008, 04:51 PM
Take a look at this first (http://www.ksproseries.com/p.htm)to see what Im trying to do. "keep in mind im trying to figure out how to do this so the top of page may be chaging from time to time with my attemps, but the bottom is what my goal is to achieve which is not php or anything simplely a display of what I want to happen.
Here what I would like to use, but insted of display each entry individgualy just display the Attend_Round ie wichita rc raceway, track2. once then count how many times each of these Eighth_Scale_Buggy, Eighth_Scale_Sportsman, Arena_Truck, Monster_Truck, Stadium_Truck accour for the value of Attend_Round.
Im sure count is the right thing to do, but have tried many differant things with no succes. I dont know where to put it in the $query= 'SELECT line i guess.


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
$query= 'SELECT Attend_Round, Eighth_Scale_Buggy, Eighth_Scale_Sportsman, Arena_Truck, Monster_Truck, Stadium_Truck FROM Entry_Form';

$result=mysql_query($query);

$num=mysql_numrows($result);

if ($num==0) {

echo "";
}else{
echo "";
}

mysql_close();

$i=0;
while ($i < $num) {

$Attend_Round=mysql_result($result,$i,"Attend_Round");
$Eighth_Scale_Buggy=mysql_result($result,$i,"Eighth_Scale_Buggy");
$Eighth_Scale_Sportsman=mysql_result($result,$i,"Eighth_Scale_Sportsman");
$Arena_Truck=mysql_result($result,$i,"Arena_Truck");
$Monster_Truck=mysql_result($result,$i,"Monster_Truck");
$Stadium_Truck=mysql_result($result,$i,"Stadium_Truck");

echo "<B>$Attend_Round</b><BR>";

if ($Eighth_Scale_Buggy != ""){
echo "$Eighth_Scale_Buggy<BR>";
}
if ($Eighth_Scale_Sportsman != ""){
echo "$Eighth_Scale_Sportsman<BR>";
}
if ($Arena_Truck != ""){
echo "$Arena_Truck<BR>";
}
if ($Monster_Truck != ""){
echo "$Monster_Truck<BR>";
}
if ($Stadium_Truck != ""){
echo "$Stadium_Truck<BR>";
}

echo "<hr>";

$i++;
}
?>