so i'm fairly new to PHP/MySQL but since network admins have more and more have the need to become web developers as well, less money more resposiblity, here i am! :queasy:
Anyway's i'm developing a resource directory... i have firm grasp on the layout, the structure of the pages and database but i'm running into a problem with linking two table and diplaying results from both tables through two unbound loops.
Dispay sub categories in an unbound loop seperated into 3 colums, easy enough (code below), problem comes into play adding a second loop from another table to the equation. Under each sub-category their is too be a i++ number of companies, i need to display these company names in a loop under each category before the loop restarts or exits completly. I've tried many trial and error solutions, no luck yet..... any suggestions on how to insert a unbound loop inside of another loop from two completly diffrent database tables?
Hope enough info provided let me know if anything else i can supply that might help point in a direction! All help is graciously appreciated, deadlines deadlines deadlines, i'll be here trial an erroring this till it's fixed 😕 THANX
Details:
Database tables(2):
1) directory - containing all ad information, name, company ect
2) subcategory - contains all the subcategories
Output of script below----> *indicates desired output
American-------- Anglo-Indian--------- Architectural
company1-------------------------------company8
company2
company3
Clocks------------English --------------- French
company5
comapny6------------------------------ *company12
Genera--------Lighting------------------Mirrors
---------------- company11
---------------- company10
---------------- company9
---------------- company13
<?php
if(isset($GET['id'])) {
// clean up sql string
$ID = htmlspecialchars($GET['id']);
$sql = "SELECT
*
FROM subcategory
WHERE cat_id = '$ID'
";
$result = @($sql, $link);
if(!$result) {
print "Could not execute your select images request";
exit;
}
$num = @mysql_num_rows($result);
// you choose how many columns you want to display in each table row
$thumbcols = 3;
// quick and dirty formula to figure out how many rows you will need
$thumbrows = 1+ round($num / $thumbcols);
?>
</div>
<?php
print '<table border="0" cellpadding="5" cellspacing="0">';
if(!empty($num)) {
// if the query is successful print out a row of table beta
// function to display multiple table cells before starting a new row
function display_table() {
// make variables available outside of the function
global $num, $result, $thumbrows, $thumbcols;
// format the number of rows to be printed using a loop
for($r=1;$r<=$thumbrows;$r++) {
print '<tr>';
// format the number of columns to be printed in each row using a 2nd for loop
for($c=1;$c<=$thumbcols;$c++) {
print '<td bgcolor="#ffffff" align="center" valign="bottom">';
$row = @mysql_fetch_array($result); // break out the array of values from the returned query
$cat_id = $row['cat_id'];
$subcategory = $row['subcategory'];
$path = '../new/uploads/healthcare/pic1id';
$ext = '.jpg';
if(!empty($cat_id)) {
// echo the actual data to the screen
print
"
<br>
$subcategory
";
}
else {
print ' ';
}
print '</td>';
}
print '</tr>';
}
}
display_table(); // call function to do this wonderful thing
print '</table>';
}
}
?>