One Query, Selecting Muliple Tables...
07-14-2011, 06:32 PM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,301
Name: ...
Location: ...
|
Quote:
Originally Posted by mgraphic
You are missing a ; on the line before it
|
Now I am getting this error:
Code:
syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING i
Referring line:
PHP Code:
while ( $row = mysql_fetch_array($subcats_query) ) echo "<li>$row['subcategory_name']";
__________________
Made2Own
|
|
|
|
07-14-2011, 06:41 PM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
|
When you enclose array vars in double quotes, you need to wrap them in {}
echo "<li>{$row['subcategory_name']}";
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-14-2011, 06:57 PM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,301
Name: ...
Location: ...
|
Quote:
Originally Posted by mgraphic
When you enclose array vars in double quotes, you need to wrap them in {}
echo "<li>{$row['subcategory_name']}";
|
Now I get the same (Whitespace) error on this line:
PHP Code:
while ( $row = mysql_fetch_array($cat_query) ) echo "<li><a href=categories.php?id=$row['category_id']>$row['category_name']</a>";
Note I tried adding the open curly bracket just before the $row['category_id'] and the closing curly bracket right after the ] on category_name, and another error.
__________________
Made2Own
Last edited by Brian07002; 07-14-2011 at 06:59 PM..
|
|
|
|
07-14-2011, 07:12 PM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
|
PHP Code:
while ( $row = mysql_fetch_array($cat_query) ) echo "<li><a href=categories.php?id={$row['category_id']}>{$row['category_name']}</a>";
http://www.php.net/manual/en/languag...string.parsing
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-14-2011, 09:18 PM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,301
Name: ...
Location: ...
|
Ok, I have figured that out, Finally. Now, your still with me on this I hope. Now what I can't seem to figure out is with the multi column code to add to it. I think the query name needs some changing, but I am having problems with it.
Here's the two pieces of code that I am trying to join:
Script #1:
PHP Code:
<?php
$link = mysql_connect("localhost", "user", "pass"); mysql_select_db("duckster_categories", $link);
$query = "SELECT * from categories"; $result=mysql_query($query); $cols=4; // Here we define the number of columns echo "<table align=\"center\">"; // The container table with $cols columns do{ echo "<tr>"; for($i=1;$i<=$cols;$i++) { // All the rows will have $cols columns even if the records are less than $cols
$row=mysql_fetch_array($result); if($row){ // $img = $row['image_path']; ?>
<td> <table> <tr valign="top"> <!-- <td><img src="http://tycoontalk.freelancer.com/images/<?=$img ?>" /></td> <!-- columns can have both text and images --> <td> <b><?="<a href=\"$row[url]\">".$row['category_name']?></a></a></b>
<?=$row['email'] ?><br /> <?=$row['password'] ?><br /> </td> <td width="50"> </td> <!-- Create gap between columns --> </tr> </table> </td>
<?php } else{ echo "<td> </td>"; //If there are no more records at the end, add a blank column } } } while($row); echo "</table>"; ?>
Script #2:
PHP Code:
<?php
// create conection to DB
$db_link = mysql_connect("localhost", "user", "pass"); $db_name = mysql_select_db("duckster_categories");
if ( isset($_GET['id']) && is_numeric($_GET['id'] )) {
$cat_id = $_GET['id']; $subcats_query_sql = "SELECT * FROM categories, subcategories WHERE categories.category_id = $cat_id AND categories.category_id = subcategories.subcategory_cat_id"; $subcats_query = mysql_query($subcats_query_sql);
echo "<ul>"; while ($row = mysql_fetch_array($subcats_query) ) echo "<li>{$row['subcategory_name']}"; echo "</ul>";
// Outputs you a list of subcategories in this category
} else {
$cat_query_sql = "SELECT * FROM categories"; $cat_query = mysql_query($cat_query_sql);
echo "<ul>"; while ( $row = mysql_fetch_array($cat_query) ) echo "<li><a href=\"categories.php?id={$row['category_id']}\">{$row['category_name']}</a></li>"; echo "</ul>";
// This is the categories list
}
?>
Basically, I just want to have one page, where the categories with their correct links (which is already done in script #2) are show in multiple columns (as in script #1), instead of a list as it currently is.
Thank you again!
Brian
__________________
Made2Own
Last edited by Brian07002; 07-14-2011 at 09:23 PM..
|
|
|
|
07-14-2011, 09:33 PM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
|
If you want the table layout from script 1, just copy the block of code that builds the table data layout from script 1 between the else { . . . } brackets in script 2
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-14-2011, 10:10 PM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,301
Name: ...
Location: ...
|
Quote:
Originally Posted by mgraphic
If you want the table layout from script 1, just copy the block of code that builds the table data layout from script 1 between the else { . . . } brackets in script 2
|
Did that, all I get is a blank page.
Here's what I did:
PHP Code:
// Outputs you a list of subcategories in this category
} else {
echo "<table align=\"center\">"; // The container table with $cols columns do{ echo "<tr>"; for($i=1;$i<=$cols;$i++) { // All the rows will have $cols columns even if the records are less than $cols
$row=mysql_fetch_array($result); if($row){ // $img = $row['image_path']; ?>
<td> <table> <tr valign="top"> <!-- <td><img src="http://tycoontalk.freelancer.com/images/<?=$img ?>" /></td> <!-- columns can have both text and images --> <td> <b><?="<a href=\"$row[url]\">".$row['category_name']?></a></a></b>
<?=$row['email'] ?><br /> <?=$row['password'] ?><br /> </td> <td width="50"> </td> <!-- Create gap between columns --> </tr> </table> </td>
<?php } else{ echo "<td> </td>"; //If there are no more records at the end, add a blank column } } } while($row); echo "</table>";
// This is the categories list
}
__________________
Made2Own
Last edited by Brian07002; 07-14-2011 at 10:12 PM..
|
|
|
|
07-15-2011, 08:07 AM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
|
You forgot the query and define the number of columns
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-15-2011, 09:58 AM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,301
Name: ...
Location: ...
|
Quote:
Originally Posted by mgraphic
You forgot the query and define the number of columns
|
Ok, I've added the query, and the # of columns, now I am getting this error:
Code:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in categories.php
PHP Code:
<?php
// create conection to DB
$db_link = mysql_connect("localhost", "user", "pass");
$db_name = mysql_select_db("db");
if ( isset($_GET['id']) && is_numeric($_GET['id'] )) {
$cat_id = $_GET['id'];
$subcats_query_sql = "SELECT * FROM categories, subcategories WHERE categories.category_id = $cat_id AND categories.category_id = subcategories.subcategory_cat_id";
$subcats_query = mysql_query($subcats_query_sql);
echo "<ul>";
while ($row = mysql_fetch_array($subcats_query) ) echo "<li>{$row['subcategory_name']}";
echo "</ul>";
// Outputs you a list of subcategories in this category
} else {
$cat_query_sql = "SELECT * FROM categories";
$cat_query = mysql_query($cat_query_sql);
$cols=4; // Here we define the number of columns
echo "<table align=\"center\">"; // The container table with $cols columns
do{
echo "<tr>";
for($i=1;$i<=$cols;$i++) {
// All the rows will have $cols columns even if the records are less than $cols
$row=mysql_fetch_array($result);
if($row){
// $img = $row['image_path'];
?>
<td>
<table>
<tr valign="top">
<!-- <td><img src="http://tycoontalk.freelancer.com/images/<?=$img ?>" /></td> <!-- columns can have both text and images -->
<td>
<?=$row['email'] ?><br />
<?=$row['password'] ?><br />
</td>
<td width="50"> </td> <!-- Create gap between columns -->
</tr>
</table>
</td>
<?php
}
else{
echo "<td> </td>"; //If there are no more records at the end, add a blank column
}
}
}
while($row);
echo "</table>";
echo "<ul>";
while ( $row = mysql_fetch_array($cat_query) ) echo "<li><a href=\"categories.php?id={$row['category_id']}\">{$row['category_name']}</a></li>";
echo "</ul>";
// This is the categories list
}
?>
__________________
Made2Own
|
|
|
|
07-15-2011, 10:37 AM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
|
You are calling mysql_fetch_array($result) when $result does not exist. Your query is assigned to the var $cat_query
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-15-2011, 11:40 AM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,301
Name: ...
Location: ...
|
Quote:
Originally Posted by mgraphic
You are calling mysql_fetch_array($result) when $result does not exist. Your query is assigned to the var $cat_query
|
I really don't know the correct syntax, I need to relearn alot of my php, but this category index needs to be working...I hate to be a pain, I will learn it, but I need to learn from working examples.
__________________
Made2Own
|
|
|
|
07-15-2011, 11:54 AM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
|
Edit mysql_fetch_array($result) to mysql_fetch_array($cat_query)
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-15-2011, 03:35 PM
|
Re: One Query, Selecting Muliple Tables...
|
Posts: 2,301
Name: ...
Location: ...
|
Quote:
Originally Posted by mgraphic
Edit mysql_fetch_array($result) to mysql_fetch_array($cat_query)
|
Thank you mgraphic for your continuing help on this issue. I have got it working.
__________________
Made2Own
|
|
|
|
|
« Reply to One Query, Selecting Muliple Tables...
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|