Jump to content

php select code display products across page ?


lill77

Recommended Posts

Hello ive been helped before and am knew to this i really want to display products acroos the page by 4 then contnuing down by ABC.. My code is below can anybody shed some light for me.  ??? ???

 

<?php
include 'admin/db.php'; 

include 'header-home.php';

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");


// create query
$query = "SELECT * FROM `products` ORDER BY 'CAT' ASC";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
     
    while($row = mysql_fetch_array($result)) {
          echo "<tr><td align='left' valign='top'><a href='search.php?cat=$row[iD]'>$row[CAT]</a> </tr>";



    }
  
}
else {
    // no
    // print status message
    echo "<p align='center'><font color='#808080' face='Arial'>No records found.</font></p>";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>

<?php


// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");


// create query
if (isset($_REQUEST['search'])) 
{
$query = "SELECT * FROM  products WHERE description LIKE  '%".$_REQUEST['search']."%'";
}
else if (isset($_REQUEST['cat'])) 
{
$query = "SELECT * FROM products WHERE category_id = ".$_REQUEST['cat']." ORDER BY Title ASC";
} 

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if ($num=mysql_num_rows($result) > 0) {
    
// yes
    // print them one after another
ECHO "<table width='90%' height='257' border='0'>";


    while($row = mysql_fetch_array($result)) {

?> <p class="style13"> </p>
          <table width="168" border="0" align="center">
          <tr>
            <td><div align="center"><span class="style13"><? echo $row['title']; ?></span></div></td>
          </tr>
          <tr>
            <td><a href='productimages/<?=$row[5];?>
            
            ' rel="lightbox"><img src="productimages/<?=$row[6];?>" width="133" height="180" /></td>
          </tr>
          <tr>
            <td><? echo $row['description']; ?> <div align="center"></div>
            <div align="center"></div></td>
          </tr>
          <tr>
            <td><div align="center"><span class="style6">Size: <? echo $row['size']; ?></span></div></td>
          </tr>
        </table>          <p> </p></td>
      </tr><tr bgcolor="#6699FF"><td height="20" valign="top" bgcolor="#FFFFFF"> </td>
      </tr>
      <p>
        <?
}
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

include 'footer.php';?>

Link to comment
Share on other sites

lill77,

 

    You will want tro try using the modulo result from the '%' operator.

 

    Run the following to see the desired effect.

 

    It is generic, but should set you on the right path....

 

Scot L. Diddle, Richmond VA

 

<?php

/* Output: 

row 1  1   2   3  
row 2  4   5   6  
row 3  7   8   9  
row 4  10  11  12 

*/


$somePHPArray = array();

$somePHPArray = range(1,12);

$rowCount = 1;

echo "\t<table border =\"1\" width=\"210\" height=\"150\" cellpadding=\"0\" cellspacing=\"0\" style=\"font-size:11px; font-family:Verdana; margin:5px;\">\r\n \n";

foreach($somePHPArray as $IDX => $arrayItemValue) {

	$modulo = $IDX % 3;

	if ($modulo === 0) { // Only 3 items per row ( besides the "Row Count" Identifier )

		if ($IDX > 0) { 		  // don't write closeing </tr> on first pass...
			echo "\t\t</tr> \n";
		}

		echo "\t\t<tr> \n";

		echo "\t\t\t<td style=\"padding: 5,5,5,5;\"> \n";
		echo "\t\t\t\trow " . $rowCount . " \n";        // Once per row

		$rowCount++;

	}

	echo "\t\t\t<td style=\"padding: 5,5,5,5;\"> \n";
	echo "\t\t\t\t$arrayItemValue \n";
	echo "\t\t\t</td> \n";


}

echo "\t</table> ";

?>

Link to comment
Share on other sites

lill77,

 

    It should be modified for your environment, then replace your table logic with your modified version...  What I think you are trying to do is to create a new row after x number of records ( columns ? ) have been written.

 

In my example, a new row is written after every 3 data points... :

 

    Whenever " $IDX % 3  " has the result = 0, then it is time to create a new row ( close current "<tr> " with </tr> then start a new row with <tr>... after ever third <td>value</td> pair has been created.

 

Change 3 to 4 and you will get four items on each row before a new row is created.... (and so on...)

 

The following :

 


<?php 


$t1 = 0 % 3;
$t2 = 1 % 3;
$t3 = 2 % 3;
$t4 = 3 % 3;

echo "\$t1 = 0 % 3; :: " . $t1 . "<br /> <br /> \n";
echo "\$t2 = 1 % 3; :: " . $t2 . "<br /> <br /> \n";
echo "\$t3 = 2 % 3; :: " . $t3 . "<br /> <br /> \n"; 
echo "\$t4 = 3 % 3; :: " . $t4 . "<br /> <br /> \n";

exit;

?>

 

    Produces:

 

        $t1 = 0 % 3; :: 0

 

        $t2 = 1 % 3; :: 1

 

        $t3 = 2 % 3; :: 2

 

        $t4 = 3 % 3; :: 0

 

    Replace my : " foreach($somePHPArray as $IDX => $arrayItemValue) { " statement with

 

    your : "  while($row = mysql_fetch_array($result)) { "

 

    Then, replace my table statements with your modifed version which has the new modulo logic.

 

Hope this helps.

 

Scot

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.