Jump to content

SQL table question


boo_lolly

Recommended Posts

if i have a SQL table with, i dunno, 7 columns in it. how do i write a script to categorize all the rows with the same name in their first column? and then write a loop to output the info into an HTML table, with the name as the title of the table, then mysql_fetch_array() the rest of the info in all of those rows. as many times as there are rows that match that first column. and then, once it's done with the first set, it will match another set of names in the first column, and do the same with those. until the last row has been called. then the loop stops.

i can write the output, no problem. but i need help writing a script to categorize the SQL rows into sets. the rows will be categorized together if the first column in their row matches another row's first column. i can pretty much do the rest myself. thanks.
Link to comment
https://forums.phpfreaks.com/topic/27240-sql-table-question/
Share on other sites

You can try something like this....

[code]<?php

//connect to db

$sql = "SELECT * FROM `table_name` ORDER BY column_one";
$result = mysql_query($query);

$first_time = TRUE;
$old = "s0me_value_that you wont see in the database 4 sure!! 1234";
while($row = mysql_fetch_array($result))
{
if($row['column_one'] == $old)
echo "<tr><td>".$row['column_two']."</td><td>".$row['column_three']."</td>....</tr>";

else
{
if(!$first_time)
{
echo "</table>";
}
echo "<center><b>".$row['column_one']."</b></center><br><br>";
echo "<table><tr><td>col1</td><td>col2</td>....</tr>";
echo "<tr><td>".$row['column_two']."</td><td>".$row['column_three']."</td>....</tr>";
$first_time = FALSE;
$old = $row['column_one'];
}
}

?>[/code]

Orio.
Link to comment
https://forums.phpfreaks.com/topic/27240-sql-table-question/#findComment-124561
Share on other sites

that helps a lot bro. but maybe i'm not explaining the issue properly. imagine a SQL table with 4 columns. the first column is named 'category'. the second is named 'item'. the other two, 'size', 'color'. (attributes of the item)

i want to write a script that will find all the matching categories, and output an HTML table with the title of the category, then list the items and attributes in that category. here's what the SQL table looks like...

[b]Category      Item                Size        Color[/b]
Plates          Glass            Medium      Red
Linens          Drapes          Large      Grey
Linens          Bed Sheet    Medium    White
Plates          Plastic          Large        White
Linens          Napkins        Small        White

the content of the SQL table will NOT be in order. i want to write a loop of somekind (or nested loop) that will find all the categories, match them, then put out an HTML table as shown below.

[b]Category: Plates[/b]
[b]Item            Size        Color[/b]
Glass        Medium      Red
Plastic        Large        White

[b]Category: Linens[/b]
[b]Item            Size        Color[/b]
Drapes        Large      Grey
Bed Sheet  Medium    White
Napkins      Small        White

that's what i want the HTML tables to look like. does that make sense?
Link to comment
https://forums.phpfreaks.com/topic/27240-sql-table-question/#findComment-124599
Share on other sites

what do you think of this? see any problems with this code?
[code]
<?php
//registry.php

@ $db = mysql_connect("host", "registry_DB", "pass");
if(!$db)
{
echo "Error: Could not connect to the database. Please try again later.";
exit;
}

//newlywed's info
$sql = mysql_query("SELECT ". $uID ." FROM my_search_DB WHERE uID") or die(mysql_error());
$result = mysql_query($sql);

$row = mysql_fetch_array($result);

echo "<B>Bride and Groom's Name: </B>". $row['brideFname'] ." ". $row['brideLname'] ." & ". $row['groomFname'] ." ". $row['groomLname'] ."<br /><br />";
echo "<B>Event Date: </B>:". $row['event_month'] ."/". $row['event_day'] ."/". $row['event_year'] ."<br /><br />";
echo "<B>Preferred Shipping Address: </B>:". $row['ship_street'] .", ". $row['ship_city'] .", ". $row['ship_state'] .", ". $row['ship_zip'] ."<br /><br />";
mysql_close($sql);



$sql = mysql_query("SELECT * FROM ". $uID ." ORDER BY category") or die(mysql_error());
$row = mysql_query($sql);

$num_rows = mysql_num_rows($result);

if(!$result)
{
echo "There are no items in the registry for this couple at this time. Please try again later.";  //connect to search_DB to add newlywed's names instead of 'this couple'
}
else
{
$row = mysql_fetch_array($result);

for($i = 0; $i < $numrows; $i == $i) //<-- no increment here. look inside while loop for increment.
{
echo "<TABLE BORDER=1><TR><TH COLSPAN=7>Category: ". $row['category'] ."</TH></TR>";
echo "<TR><TH>Item</TH><TH>Quantity Requested</TH><TH>Still Needs</TH><TH>Price</TH><TH>View</TH><TH>Quantity</TH><TH>Buy</TH></TR>";

while($row['category'] === $row['category']) //<-- pretty sure this will stop the while loop if it hits a new category.
{
echo "<TR><TD>". $row['item'] ."</TD><TD>". $row['qty_req'] ."</TD><TD>". $row['still_need'] ."</TD><TD>". $row['price'] ."</TD><TD>". $row['view'] ."</TD><TD>Input Field</TD><TD>Add to Cart</TD></TR>";
                                $i++;
}
echo "</TABLE><br /><br />";
}
}
mysql_close($sql);
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/27240-sql-table-question/#findComment-124641
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.