 |
10-03-2006, 08:31 PM
|
DB count thing
|
Posts: 270
|
I have this script where i print some groups, and the want to add the totalt number of items in each group behind the number... like "Cars(9), Music(12)"
I need to work on two tables, my group table and the table where i keep the listing of items.
The scrip i have now prints something that makes no sence:
Music(Resource id #6) - Computers(Resource id #8) - Shooes(Resource id #10) -
This is the script:
PHP Code:
<?PHP //Server connect -------------------------------------
$count1=0; $group = mysql_db_query("$datab", "select id, navn, active from group where active=1 order by navn DESC");; while ($row = mysql_fetch_array ($group)) { $id = "".$row["id"]; $groupname= "".$row["navn"]; $groupid= "".$row["groupid"]; $active = "".$row["active"]; $count=$count1++;
$items3 = mysql_db_query("$datab", "select id, lutnavn, bolkid, active from tilbud where active=1");; while ($row = mysql_fetch_array ($items3)) { $id22 = "".$row["id"]; $bolkid22 = "".$row["bolkid"]; } $count1 = mysql_query("SELECT COUNT(*) FROM tilbud WHERE groupid= $groupid22");
print("<a href=\"?group=$id\" style=\"text-decoration:none\">$groupname($count1) - </em></font></a>"); print ($count1 % 3==0) ? "<br>" : "" ; } ?>
Translated bits of this from faroese into english, so if you spot some typing error, it is has probalby happend during the translation.
How do i only get the a number out of the count, instead of the way it looks like explained at the start... BTW the numbers in the print makes no sence ... like Music(Resource id #6) - 6 is not the number of items in Music - there is nothing in that group
Hope someone can help
|
|
|
|
10-04-2006, 03:18 AM
|
Re: DB count thing
|
Posts: 128
Name: irimia octavian
Location: Romania
|
trie to take the number from mysql
"SELECT SUM(row) AS row_num"
and into PHP print $you['row_num'];
|
|
|
|
10-04-2006, 07:02 AM
|
Re: DB count thing
|
Posts: 270
|
did not get it to work..... how would that loook like when in my script ?
|
|
|
|
10-04-2006, 07:25 AM
|
Re: DB count thing
|
Posts: 551
Name: surajit ray
Location: inside the heart of my friends
|
tell briefly about ur tables.
__________________
Think+, work +, but not HIV + :)
|
|
|
|
10-04-2006, 07:30 AM
|
Re: DB count thing
|
Posts: 270
|
I have a table with group names (id, name)
I have a table with items wich contains listed items
|
|
|
|
10-04-2006, 07:37 AM
|
Re: DB count thing
|
Posts: 551
Name: surajit ray
Location: inside the heart of my friends
|
this is ur select statement :
Quote:
|
...select id, navn, active from group where active=1 order by navn DESC");
|
then from where you get $row["groupid"].
__________________
Think+, work +, but not HIV + :)
|
|
|
|
10-04-2006, 07:52 AM
|
Re: DB count thing
|
Posts: 46
Name: Sid
|
You could try this:
I presume that:
1. groupid of table tilbud is the id in table group .
2. And you only want the count of listing under each category.
The code boradly looks like this.
$group = q("select id from group where active=1");
foreach($group[id])
{
// A comma seperated array of category listings count.
$count_list.=q ("SELECT COUNT(*) FROM tilbud WHERE groupid= $id").",";
}
Last edited by StickNoBills : 10-04-2006 at 07:58 AM.
|
|
|
|
10-04-2006, 10:50 AM
|
Re: DB count thing
|
Posts: 270
|
Quote:
Originally Posted by StickNoBills
You could try this:
I presume that:
1. groupid of table tilbud is the id in table group .
2. And you only want the count of listing under each category.
The code boradly looks like this.
$group = q("select id from group where active=1");
foreach($group[id])
{
// A comma seperated array of category listings count.
$count_list.=q ("SELECT COUNT(*) FROM tilbud WHERE groupid= $id").",";
}
|
I think you have got it right, but the code gives an error yet..... it says:
Parse error: syntax error, unexpected ')' in /usr/home/web/web128130/kikarin/tilbudbolkar.php on line 25
The scipt now looks like this(nothing translated into english this time):
PHP Code:
<?PHP //Server connect ------------------------------------- include '/usr/home/web/xxxxxxxxxxxxxx/xxxxxx/xxxxxxxx/xxxxxx/xxxxxx.php';
$count1=0; $weather2 = mysql_db_query("$datab", "select id, navn, active from tilbudbolkar where active=1 and id BETWEEN 1 and 7 order by navn DESC");; while ($row = mysql_fetch_array ($weather2)) { $id = "".$row["id"]; $navn = "".$row["navn"]; $bolkid = "".$row["bolkid"]; $active = "".$row["active"]; $count=$count1++;
$weather3 = mysql_db_query("$datab", "select id, lutnavn, bolkid, active from tilbud where active=1");; while ($row = mysql_fetch_array ($weather3)) { $id22 = "".$row["id"]; $bolkid22 = "".$row["bolkid"]; }
$group = q("select id from tilbudbolkar where active=1"); foreach($group[id]) { // A comma seperated array of category listings count. $count_list.=q ("SELECT COUNT(*) FROM tilbud WHERE bolkid= $id").","; } print("<a href=\"?tilbudbolkur=$id\" style=\"text-decoration:none\">$navn($count_list) - </em></font></a>"); print ($count1 % 1==0) ? "<br>" : "" ; } ?>
|
|
|
|
10-04-2006, 11:18 AM
|
Re: DB count thing
|
Posts: 46
Name: Sid
|
Tomcat, sorry, i didn't mean to be syntactically right when I had given the code. I will try and post the code tomorrow, should you need it.
|
|
|
|
10-04-2006, 12:36 PM
|
Re: DB count thing
|
Posts: 46
Name: Sid
|
Try this code:
$groups = mysql_db_query("$datab", "select id from group where active=1");
while ($row = mysql_fetch_array ($groups))
{
$count = mysql_query("SELECT COUNT(*) AS total_count FROM tilbud WHERE groupid = $row["id"]");
while ($row_count = mysql_fetch_array ($count))
{
$count_array.=$row_count["total_count"].",";
}
}
echo $count_array;
|
|
|
|
10-04-2006, 12:57 PM
|
Re: DB count thing
|
Posts: 270
|
Well now i got something (a lot).... needed to change the name of a VAR $count to $count2 because bacause the VAR was allready in use.
Her is the print:
Code:
0,0,0,0,0,1,0,1,0,0,0,0,0,Sjónvørp & tilhoyr(Resource id #19) -
0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,Ljóðútgerð(Resource id #33) -
0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,Klæðir(mannfólk)(Resource id #47) -
0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,Klæðir(konufólk)(Resource id #61) -
0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,Klæðir(børn)(Resource id #75) -
0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,Hvítvørður(Resource id #89) -
0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,Amboð(Resource id #103) -
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /usr/home/web/XXXXXXXXX/XXXXXXXXX/tilbudviewlist.php on line 10
The scipt looks like this (nothing translated):
PHP Code:
<?PHP //Server connect ------------------------------------- include '/usr/home/web/xxxxxxx/xxxxxxx/xxxxxxx/xxxxxx/xxxxxxx.php';
$count1=0; $weather2 = mysql_db_query("$datab", "select id, navn, active from tilbudbolkar where active=1 and id BETWEEN 1 and 7 order by navn DESC");; while ($row = mysql_fetch_array ($weather2)) { $id = "".$row["id"]; $navn = "".$row["navn"]; $bolkid = "".$row["bolkid"]; $active = "".$row["active"]; $count=$count1++;
$groups = mysql_db_query("$datab", "select id from tilbudbolkar where active=1"); while ($row = mysql_fetch_array ($groups)) { $count2 = mysql_query("SELECT COUNT(*) AS total_count FROM tilbud WHERE bolkid = '".$row["id"]."'"); while ($row_count = mysql_fetch_array ($count2)) { $count_array.=$row_count["total_count"].","; } } echo $count_array; print("<a href=\"?tilbudbolkur=$id\" style=\"text-decoration:none\">$navn($count2) - </em></font></a>"); print ($count1 % 1==0) ? "<br>" : "" ; } ?>
|
|
|
|
10-04-2006, 11:45 PM
|
Re: DB count thing
|
Posts: 46
Name: Sid
|
For testing, comment out everything (as shown below) that is not needed and see if the result printed is right.
Also, are there any subcategories for a catefory??? From the way the output array looks, I think yes.
<?PHP
//Server connect -------------------------------------
include '/usr/home/web/xxxxxxx/xxxxxxx/xxxxxxx/xxxxxx/xxxxxxx.php';
/*
$count1=0;
$weather2 = mysql_db_query("$datab", "select id, navn, active from tilbudbolkar where active=1 and id BETWEEN 1 and 7 order by navn DESC");;
while ($row = mysql_fetch_array ($weather2))
{
$id = "".$row["id"];
$navn = "".$row["navn"];
$bolkid = "".$row["bolkid"];
$active = "".$row["active"];
$count=$count1++;
*/
$groups = mysql_db_query("$datab", "select id from tilbudbolkar where active=1");
while ($row = mysql_fetch_array ($groups))
{
$count2 = mysql_query("SELECT COUNT(*) AS total_count FROM tilbud WHERE bolkid = '".$row["id"]."'");
while ($row_count = mysql_fetch_array ($count2))
{
$count_array.=$row_count["total_count"].",";
}
}
echo $count_array;
/*print("<a href=\"?tilbudbolkur=$id\" style=\"text-decoration:none\">$navn($count2) - </em></font></a>");
print ($count1 % 1==0) ? "<br>" : "" ;
}*/
?>
|
|
|
|
10-05-2006, 08:21 AM
|
Re: DB count thing
|
Posts: 128
Name: irimia octavian
Location: Romania
|
first: $group = q("select id from group where active=1"); ? alow me to laugth
second: why use $navn = "".$row["navn"]; whats the need of "". ??
2. if you've got a warning that means that the code of MySQL isn't right.
print the sql or report an error...
3. $groups = mysql_db_query("$datab", "select id from tilbudbolkar where active=1");
while ($row = mysql_fetch_array ($groups))
{
$count2 = mysql_query("SELECT COUNT(*) AS total_count FROM tilbud WHERE bolkid = '".$row["id"]."'");
while ($row_count = mysql_fetch_array ($count2))
{
$count_array.=$row_count["total_count"].",";
}
}
there are 2 whiles....
"SELECT COUNT(*) AS total_count FROM tilbud WHERE bolkid = '".$row["id"]."'"
will show you one value ! ... the count as total_count. no need to do while
just print $row_count["total_count"]
|
|
|
|
10-06-2006, 06:05 PM
|
Re: DB count thing
|
Posts: 270
|
Quote:
Originally Posted by StickNoBills
For testing, comment out everything (as shown below) that is not needed and see if the result printed is right.
Also, are there any subcategories for a catefory??? From the way the output array looks, I think yes.
<?PHP
//Server connect -------------------------------------
include '/usr/home/web/xxxxxxx/xxxxxxx/xxxxxxx/xxxxxx/xxxxxxx.php';
/*
$count1=0;
$weather2 = mysql_db_query("$datab", "select id, navn, active from tilbudbolkar where active=1 and id BETWEEN 1 and 7 order by navn DESC");;
while ($row = mysql_fetch_array ($weather2))
{
$id = "".$row["id"];
$navn = "".$row["navn"];
$bolkid = "".$row["bolkid"];
$active = "".$row["active"];
$count=$count1++;
*/
$groups = mysql_db_query("$datab", "select id from tilbudbolkar where active=1");
while ($row = mysql_fetch_array ($groups))
{
$count2 = mysql_query("SELECT COUNT(*) AS total_count FROM tilbud WHERE bolkid = '".$row["id"]."'");
while ($row_count = mysql_fetch_array ($count2))
{
$count_array.=$row_count["total_count"].",";
}
}
echo $count_array;
/*print("<a href=\"?tilbudbolkur=$id\" style=\"text-decoration:none\">$navn($count2) - </em></font></a>");
print ($count1 % 1==0) ? "<br>" : "" ;
}*/
?>
|
I think i got it to work now, i removed a dot/peroiod (.) behind $count_array. and removed a (,) between two "," - $count_array.=$row_count["total_count"].",";
Don't know what that was for.... maybe for decimal numbers ??? - i only need whole numbers.
This is how the script looks now:
PHP Code:
<?PHP //Server connect ------------------------------------- include '/usr/home/web/xxxxxxxxxxx/xxxxxxx/xxxxxxx/xxxxxxxxxxxxx/xxxxxxx.php';
$count1=0; $weather2 = mysql_db_query("$datab", "select id, navn, active from tilbudbolkar where active=1 and id BETWEEN 1 and 7 order by navn DESC");; while ($row = mysql_fetch_array ($weather2)) { $id = "".$row["id"]; $navn = "".$row["navn"]; $bolkid = "".$row["bolkid"]; $active = "".$row["active"]; $count=$count1++;
$groups = mysql_db_query("$datab", "select id from tilbudbolkar where active=1"); while ($row = mysql_fetch_array ($groups)) { $count2 = mysql_query("SELECT COUNT(*) AS total_count FROM tilbud WHERE bolkid = $id"); while ($row_count = mysql_fetch_array ($count2)) { $count_array=$row_count["total_count"].""; } }
print("<a href=\"?tilbudbolkur=$id\" style=\"text-decoration:none\">$navn($count_array) - </em></font></a>"); print ($count1 % 1==0) ? "<br>" : "" ; } ?>
Talkupation for you  - thanks a lot !
|
|
|
|
|