PDA

View Full Version : Confused in how to perform this Query-ing ..


cinq
10-31-2003, 04:24 AM
if ($action=="main") {

$result = $DB_site->query("SELECT * FROM links_cat ORDER by catorder");
if ($row = $DB_site->num_rows($result)) {
while ($row=$DB_site->fetch_array($result)) {
$catid=$row["linkcatid"];
$catname=$row["catname"];
$catbyline=$row["catbyline"];

$result1= $DB_site->query("SELECT * FROM links_content WHERE categoryid='$catid'");
if ($row = $DB_site->num_rows($result1)) {
while ($row=$DB_site->fetch_array($result1)) {
$name=$row["name"];
$byline=$row["byline"];
$content=$row["content"];
eval("\$catlistbit .= \"".gettemplate('links_catbit')."\";");
}}
}}
eval("dooutput(\"".gettemplate('links_main')."\");");
}

cinq
10-31-2003, 04:25 AM
This is the DB structure :


#
# Table structure for table 'links_cat'
#

CREATE TABLE links_cat (
linkcatid int(11) NOT NULL auto_increment,
catorder int(50) NOT NULL default '0',
catname varchar(255) NOT NULL default '',
catbyline text NOT NULL,
PRIMARY KEY (linkcatid)
) TYPE=MyISAM;

#
# Table structure for table 'links_content'
#

CREATE TABLE links_content (
linkid int(11) NOT NULL auto_increment,
categoryid varchar(255) NOT NULL default '',
name varchar(255) NOT NULL default '',
byline text NOT NULL,
content text NOT NULL,
PRIMARY KEY (linkid)
) TYPE=MyISAM;

cinq
10-31-2003, 04:28 AM
i want to display the results as follows :

Cat 1 name
- link1 belonging to cat 1
- link2 belonging to cat 1

Cat 2 name
- link3 belonging to cat 2
- link4 belonging to cat 2

Currently, the 'links_catbit' template displays info in the following format :

Cat name
- link belonging to cat
- link belonging to cat
- ....


But with the above code , I only manage to display the results for the 1st cat in my tables.

How can i display all of them ?

Any help is greatly appreciated :D

cinq
10-31-2003, 11:30 PM
anyone ? :(

Dean C
11-04-2003, 12:40 PM
Moved upon request :)

cinq
11-06-2003, 03:05 AM
No help at all ? :(

Lesane
11-06-2003, 06:29 AM
Try this:


if ($action=="main") {

$result = $DB_site->query("SELECT * FROM links_cat ORDER by catorder");
if (mysql_num_rows($result)) {
while ($row=$DB_site->fetch_array($result)) {
$catid=$row["linkcatid"];
$catname=$row["catname"];
$catbyline=$row["catbyline"];

$result1= $DB_site->query("SELECT * FROM links_content WHERE categoryid='$catid'");
if (mysql_num_rows($result1)) {
while ($row2=$DB_site->fetch_array($result1)) { // changed $row into $row2 because $row is used in the other while
$name=$row2["name"];
$byline=$row2["byline"];
$content=$row2["content"];
eval("\$catlistbit .= \"".gettemplate('links_catbit')."\";"); // edit the template, change $row into $row2
} // end while $row2
} // end if rows $result1

} // end while $row
} // end if rows $result

eval("dooutput(\"".gettemplate('links_main')."\");");
}

cinq
11-06-2003, 07:03 AM
Thanks for the reply Lesane,
however in my template i do not have a $row variable.

This is the code i am now using ( still DOESNT work )

$result = $DB_site->query("SELECT * FROM links_cat ORDER by catorder");
if ($row = $DB_site->num_rows($result)) {
while ($row=$DB_site->fetch_array($result)) {
$catid=$row["linkcatid"];
$catname=$row["catname"];
$catbyline=$row["catbyline"];

$result1 = $DB_site->query("SELECT * FROM links_content WHERE categoryid='$catid' ORDER BY name ASC ");
if ($row2 = $DB_site->num_rows($result1)) {
while ($row2=$DB_site->fetch_array($result1)) {
$name=$row2["name"];
$byline=$row2["byline"];
$content=$row2["content"];
eval("\$linkslistbit .= \"".gettemplate('links_linkbit')."\";");
}}
eval("\$catlistbit .= \"".gettemplate('links_catbit')."\";");
}}


Template :
links_catbit

<table cellpadding="{tableouterborderwidth}" cellspacing="0" border="0" bgcolor="{tablebordercolor}" {tableouterextra} width="{contenttablewidth}" align="center">
<tr>
<td>
<table cellpadding="4" cellspacing="1" border="0" {tableinnerextra} width="100%">
<tr id="cat">
<td bgcolor="{firstaltcolor}">
<normalfont><b><A NAME="$catid">$catname</b></normalfont><br><smallfont>$catbyline</smallfont><br>
</td>
</tr>
<tr><td width=500 bgcolor="{tableheadbgcolor}">
<ul>
$linkslistbit
</ul>
</td></tr>
<tr><td bgcolor="{tableheadbgcolor}">
<div align="right"><smallfont><a href="#top"><b>Back to Top</b></a></smallfont></div>
</td></tr>
</table>

</td>
</tr>
</table>


Template :
links_linkbit

<li><smallfont><a href="$content" target="_blank"><b><font color="{categoryfontcolor}">$name</font></b></a></smallfont><br><smallfont>[ $byline ]</smallfont><br>

cinq
11-06-2003, 07:05 AM
Its some error in the looping i think , because each category is showing more links than there is supposed to be in that respective category.

assassingod
11-06-2003, 01:53 PM
Not a good idea to loop a query

cinq
11-06-2003, 02:21 PM
Any other code suggestions then ?
I just need to display the details properly ( as per what i want stated above )

Pretty new at this :(

cinq
11-10-2003, 04:00 AM
help ? :(
this close to finishing up my hack :(

cinq
11-18-2003, 12:53 AM
:(

cinq
11-23-2003, 12:39 AM
hate to do this, but help anyone ? :(

Lesane
11-23-2003, 08:00 AM
This line:
eval("\$catlistbit .= \"".gettemplate('links_catbit')."\";");
is not on the right place. Try to do it first without templates and then implement it in the templates:


$result = $DB_site->query("SELECT * FROM links_cat ORDER by catorder");
if ($row3 = $DB_site->num_rows($result)) {
while ($row=$DB_site->fetch_array($result)) {
$catid=$row["linkcatid"];
$catname=$row["catname"];
$catbyline=$row["catbyline"];
echo "<p>$catname $catbyline";

$result1 = $DB_site->query("SELECT * FROM links_content WHERE categoryid='$catid' ORDER BY name ASC ");
if ($row4 = $DB_site->num_rows($result1)) {
while ($row2=$DB_site->fetch_array($result1)) {
$name=$row2["name"];
$byline=$row2["byline"];
$content=$row2["content"];
echo "<br>$name $byline";

} // end if row3
} // end while row
} // end if row4
} // end while row2

cinq
12-02-2003, 11:00 PM
Thanks Lesane.
The query works....but I am now not sure how to implement it in templates form. :(