Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop Display

Status
Not open for further replies.

smavtron

Technical User
Jul 28, 2007
17
US
I have 4 variables, namely Merchant, sub_cat, link_detail & link and want to display on the page as

Merchant

sub_cat

<a href=link>link_detail</a> <a href=link>link_detail</a>
<a href=link>link_detail</a> <a href=link>link_detail</a>

sub_cat
<a href=link>link_detail</a> <a href=link>link_detail</a>
<a href=link>link_detail</a> <a href=link>link_detail</a>

The same merchant can have different sub_cat as Computers, Electronics etc.

I want exactly the page to show as an example on Buy.com ...

TIA.
 
I have been able to put the script to be one step ahead

Code:
<?php
$columns = 4;

//change the query to get another field from the database
$query = "SELECT * FROM rss  WHERE merchant = 'Buy.com' ORDER BY `link_detail`";
$result = mysql_query($query);

$num_rows = mysql_num_rows($result);


$rows = ceil($num_rows / $columns);

//to do this display, we will need to run another loop
//this loop will populate an array with all our values
while($row = mysql_fetch_array($result)) {
    $data[] = $row['link_detail'];
    
    //store the links into an array
    $data2[] = base64_encode($row['link']);

    //store the merchant into an array
    $data3[] = $row['merchant'];

    //store the sub category into an array
    $data4[] = $row['sub_cat'];
}

echo "<br><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"90%\" align=\"center\" id=\"AutoNumber1\">\n";
echo "<tr><td height=\"14\" colspan=\"4\" class=\"rightbanner\">&nbsp;&nbsp;".$data3[$i + ($j * $rows)]."</td></tr>\n";

for($i = 0; $i < $rows; $i++) {

    echo "<TR>\n";
    
    for($j = 0; $j < $columns; $j++) {
        if(isset($data[$i + ($j * $rows)])) {

            
            //echo out the field
            echo "<TD  width=\"25%\">&nbsp;&nbsp;<a class=\"cats\" href=\"".$data2[$i + ($j * $rows)]."\"><b>".$data[$i + ($j * $rows)]."</b></a></TD>\n";
        }
    }
    echo "</TR>\n";
}
echo "</TABLE><br>\n";
?>

This shows the result as
Not able to get it to display like
where the links gets divided under sub category... like computers, electronics.

Appreciate if someone can help.

TIA.
 
What I gather from the buy.com page is a list of categories with dynamic subcategories.

If for now, all you want to do is copy what buy.com has, I'd suggest starting by the simplification of your SQL tables, whose structure should look like this:

Table_Name

Id
Merchant
Category
Sub_Category

You would then make a query per each CATEGORY, for example:

COMPUTERS

Code:
/* Example of Query */
$query_computers_list = "SELECT * FROM Table_Name WHERE Category = 'Computers' ORDER BY Sub_Category"
$computers_list = mysql_query($query_computers_list, $your_connection_var) or die(mysql_error());
$row_computers_list = mysql_fetch_assoc($computers_list);

Now within the HTML document, you could use:

Code:
<?php do { ?>

Computers
<br />
<table width="300" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td scope="col"><?php echo $row_computers_list['Sub_Category']; ?></td>
  </tr>
</table>

<?php  } while ($row_computers_list = mysql_fetch_assoc($computers_list)); ?>

This would result in something like:

COMPUTERS

Top Selling Computer
Featured Computer
Featured Desktops
Featured Notebooks
Featured Monitors
Featured Printers
Featured Memory
Featured Accessories

However, if you want do in a single move ALL the categories with ALL of their SUB Categories then that's another story.

I'd need further clarification

I hope this helped in some way

 
Kharas... thanks for the suggestion. But for this I will have to run the query times the sub_cat... and in future if more added will have to manually update the page... I would prefer "in a single move ALL the categories with ALL of their SUB Categories". Also can the same code be used for single or multiple merchants to display in the same format, on the same page.

If only one merchant selected

Merchant

sub_cat

links

if multiple merchants selected then

Merchant1

sub_cat

links

Merchant2

sub_cat

links
 
Need some help to tweek the result.....

the code modified shows

Sub_cat1 offer
Sub_cat1 offer
Sub_cat2 offer
Sub_cat2 offer

you can view at


How can I make it to display

Sub_cat1 offer
offer
Sub_cat2 offer
offer

so that it odes not repeat the sub_cat in left column

Code:
<?php
$columns = 1;

//change the query to get another field from the database
$query = "SELECT * FROM rss WHERE  merchant = ('Buy.com')";
$result = mysql_query($query);

$num_rows = mysql_num_rows($result);


$rows = ceil($num_rows / $columns);

//to do this display, we will need to run another loop
//this loop will populate an array with all our values
while($row = mysql_fetch_array($result)) {
    $data[] = $row['sub_cat'];
    
    //store the other field into an array
    $data2[] = base64_encode($row['link']);

    //store the other field into an array
    $data3[] = $row['link_detail'];
}

echo "<br><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"90%\" align=\"center\" id=\"AutoNumber1\">\n";
echo "<tr><td height=\"14\" colspan=\"4\" class=\"rightbanner\">&nbsp;&nbsp;RSS Feeds from Merchants</td></tr>\n";
echo "<tr><TD  width=\"15%\" align=\"center\">&nbsp;&nbsp;Buy.com</td><TD  width=\"75%\" align=\"center\">&nbsp;&nbsp;Offers</td></tr>\n";

for($i = 0; $i < $rows; $i++) {

    echo "<TR>\n";
    
    for($j = 0; $j < $columns; $j++) {
        if(isset($data[$i + ($j * $rows)])) {

            //echo out the field
            echo "<TR><TD  width=\"15%\">&nbsp;&nbsp;".$data[$i + ($j * $rows)]."</td><TD  width=\"75%\">&nbsp;&nbsp;<a class=\"cats\" href=\"[URL unfurl="true"]http://www.indian-town.com/test.php?feed=".$data2[/URL][$i + ($j * $rows)]."\"><b>".$data3[$i + ($j * $rows)]."</b></a></TD></tr>\n";
        }
    }
    echo "</TR>\n";
}
echo "</TABLE><br>\n";
?>

TIA.
 
jpadie, need more help to resolve..... can you help in the code with your suggestion "add some conditionals
 
jpadie,

Sorry.... have been struggling for last 2 days.... not able to make out the final output format.

Would request that please suggest with code changes in the script. It may take you only few minutes....

TIA.
 
I made changes to the code

Code:
<?php
$columns = 1;

//change the query to get another field from the database
$query = "SELECT * FROM rss WHERE  merchant = ('Buy.com')";
$result = mysql_query($query);

$num_rows = mysql_num_rows($result);


$rows = ceil($num_rows / $columns);

//to do this display, we will need to run another loop
//this loop will populate an array with all our values
while($row = mysql_fetch_array($result)) {
    $data[] = $row['sub_cat'];
    
    //store the other field into an array
    $data2[] = base64_encode($row['link']);

    //store the other field into an array
    $data3[] = $row['link_detail'];
}

echo "<br><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"90%\" align=\"center\" id=\"AutoNumber1\">\n";
echo "<tr><td height=\"14\" colspan=\"4\" class=\"rightbanner\">&nbsp;&nbsp;RSS Feeds from Merchants</td></tr>\n";
echo "<tr><TD  width=\"15%\" align=\"center\">&nbsp;&nbsp;Buy.com</td><TD  width=\"75%\" align=\"center\">&nbsp;&nbsp;Offers</td></tr>\n";

for($i = 0; $i < $rows; $i++) {

    echo "<TR>\n";
    
    for($j = 0; $j < $columns; $j++) {
        if(isset($data[$i + ($j * $rows)])) {

            //echo out the field
            echo "<TR><TD  width=\"15%\">&nbsp;&nbsp;".$data[$i + ($j * $rows)]."</td><TD  width=\"75%\">&nbsp;&nbsp;<a class=\"cats\" href=\"[URL unfurl="true"]http://www.xxxx.com/test.php?feed=".$data2[/URL][$i + ($j * $rows)]."\"><b>".$data3[$i + ($j * $rows)]."</b></a></TD></tr>\n";
        }
    }
    echo "</TR>\n";
}
echo "</TABLE><br>\n";
?>

You can see at
Need help now only is that for a set of links the sub_cat only once. So for example Featured shows once instead of 3 times, Computers shows once and...

Please suggest.

TIA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top