Jump to content

php/mysql help


chris2010

Recommended Posts

i got a php script that list a row of items up to 6 in a single row from mysql

and but i would like it to display two side by side and then make a new row

and list two side by side again. "this for an online store"  could any help im not that good at php or mysql :(

 

i also up load a photo of why it list the  data now

 

here my code

 

for viewitems.php

http://pastebin.com/tfdAB67U

 

and for the template for viewitems.php

 

http://pastebin.com/LYyxV7rR

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

To display results in multiple columns have a look at this FAQ.

 

 

how can i put this code in my soo it do what i need, chould some show me how to or put it in if it aint much trouble

 

<?php
$cols = 0;
  echo "<table><tr>";
  while ($cols < 20)
   {
   echo ($cols % 2 == 0)? "</tr><tr>" : "";
   echo "<td>$cols</td>";
   $cols++;
      }
       echo "</tr></table>"

       ?>

 

 

 

     

 

 

 

 

 

Link to comment
Share on other sites

sorry i give the wrong code here the right code it wasen viewitem.php

i mant put the code up for ShowCategory.php i had to made pages open when i did this post and i lost track sorry.

 

 



ShowCategory.php
that show the products 
<?
require_once("conn.php");
require_once("header.php");

if(!empty($_GET[start]))
{
$Start = $_GET[start];
}
else
{
$Start = '0';
}

$ByPage = "10";


if(empty($_GET[subcategoryID]))
{

$q1 = "select devbg_products.*, devbg_categories.*, devbg_subcategories.* from devbg_products
						left join devbg_categories on devbg_products.ItemCategory = devbg_categories.CategoryID
						left join devbg_subcategories on devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID

						where devbg_products.ItemCategory = '$_GET[CategoryID]'

						order by ItemName limit $Start, $ByPage";

$qnav = "select devbg_products.*, devbg_categories.*, devbg_subcategories.* from devbg_products
						left join devbg_categories on devbg_products.ItemCategory = devbg_categories.CategoryID
						left join devbg_subcategories on devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID

						where devbg_products.ItemCategory = '$_GET[CategoryID]' ";
}
else
{

$q1 = "select devbg_products.*, devbg_categories.*, devbg_subcategories.* from devbg_products
						left join devbg_categories on devbg_products.ItemCategory = devbg_categories.CategoryID
						left join devbg_subcategories on devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID

						where devbg_products.ItemCategory = '$_GET[CategoryID]' and
								  devbg_products.ItemSubcategory = '$_GET[subcategoryID]'

						order by ItemName limit $Start, $ByPage";

$qnav = "select devbg_products.*, devbg_categories.*, devbg_subcategories.* from devbg_products
						left join devbg_categories on devbg_products.ItemCategory = devbg_categories.CategoryID
						left join devbg_subcategories on devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID

						where devbg_products.ItemCategory = '$_GET[CategoryID]' and
							  devbg_products.ItemSubcategory = '$_GET[subcategoryID]' ";
}



$r1 = mysql_query($q1) or die(mysql_error());

if(mysql_num_rows($r1) == '0')
{
$NoItems = "<center>There are no items at this category!</center>";
}
else
{


while($a1 = mysql_fetch_array($r1))
{
$MyPrice = number_format($a1[itemPrice], 2, ".", "");

if(!empty($a1[itemImage]))
{
	$xy = getimagesize("items_images/$a1[itemImage]");
	$x = $xy[0];
	$y = $xy[1];

	$NewItemName = str_replace("'", "", $a1[itemName]);

	$DisplayImage = "<a class=\"ItemTitle\" href=\"ViewItem.php?CategoryID=$a1[itemCategory]&ItemID=$a1[itemID]\"><img src=\"items_images/$a1[itemImage]\" width=100 alt=\"$a1[itemName]\" border=0 style=\"border-color:black\"></a>";
}
else
{
	$DisplayImage = " ";
}

$DisplayCategory .= "
<table align=center width=300 border=0 rules=rows cellspacing=0 bordercolor=black>
<tr>
	<td valign=top width=140>$DisplayImage</td>

	<td width=160 valign=top>
			<a class=\"ItemTitle\" href=\"ViewItem.php?CategoryID=$a1[itemCategory]&ItemID=$a1[itemID]\">$a1[itemName]</a><BR>
				<br>
			<font color=black face=verdana size=1><b>Category: </b></font><a class=\"CategoryClass\" href=\"ShowCategory.php?CategoryID=$a1[itemCategory]\">$a1[CategoryName]</a>";

	if(!empty($a1[subcategoryID]))
	{
		$DisplayCategory .= "<br><font color=black face=verdana size=1><b>Subcategory: </b></font><a class=\"SubcategoryClass\" href=\"ShowCategory.php?CategoryID=$a1[itemCategory]&SubcategoryID=$a1[itemSubcategory]\">$a1[subcategoryName]</a>";
	}

	$DisplayCategory .= "
        <br>
                <font color=red face=verdana size=2><b>Color: $a1[Color]</b></font>
                  <br>
                  <font color=red face=verdana size=2><b>Size: $a1[size]</b></font>
                  <br>
                <font color=red face=verdana size=2><b>Price: $$MyPrice</b></font>


			<br><br>

			<form method=post action=add.php style=\"margin:0\">
				<input type=hidden name=ItemID value=\"$a1[itemID]\">
				<input type=hidden name=ItemName value=\"$a1[itemName]\">
				<input type=hidden name=ItemShipping value=\"$a1[itemShipping]\">
				<input type=hidden name=ItemPrice value=\"$MyPrice\">
				<input type=submit name=s1 value=\"Add to card\" class=sub>

			</form>


		</td>
</tr>

</table><br><hr size=1 color=E3E3E3>";


}


//build the "next" - "prev" navigatioin

$rnav = mysql_query($qnav) or die(mysql_error());
$rows = mysql_num_rows($rnav);

if($rows > $ByPage)
{
		$NextPrev =  "<br><table align=center width=600>";
		$NextPrev .= "<td align=center><font face=verdana size=2> | ";

		$pages = ceil($rows/$ByPage);

		for($i = 0; $i <= ($pages); $i++)
		{
			$PageStart = $ByPage*$i;

			$i2 = $i + 1;

			if($PageStart == $Start)
			{
				$links[] = " <span class=DNav>$i2</span>\n\t ";
			}
			elseif($PageStart < $rows)
			{
				$links[] = " <a class=Nav href=ShowCategory.php?Start=$PageStart&ByPage=$ByPage&CategoryID=$_GET[CategoryID]&SubcategoryID=$_GET[subcategoryID]>$i2</a>\n\t ";
			}
		}

		$links2 = implode(" | ", $links);

		$NextPrev .= $links2;

		$NextPrev .=  "| </td>";

		$NextPrev .= "</table><br>\n";
}

}

if(isset($NoItems))
{
echo $NoItems;
}
else
{
require_once("templates/ShowCategoryTemplate.php");
}
require_once("footer.php");
?>

Link to comment
Share on other sites

i  had post of this befor but i give the wrong code sorry for that

here is the right one. thank you all for you help in here.

 

i got a php script that list a row of items up to 6 in a single row from mysql

and but i would like it to display two side by side and then make a new row

and list two side by side again. "this for an online store"  could any help im not that good at php or mysql

 

i also up load a photo of why it list the  data now

 

here my code

ShowCategory.php

 

<?
require_once("conn.php");
require_once("header.php");

if(!empty($_GET[start]))
{
$Start = $_GET[start];
}
else
{
$Start = '0';
}

$ByPage = "5";


if(empty($_GET[subcategoryID]))
{	

$q1 = "select devbg_products.*, devbg_categories.*, devbg_subcategories.* from devbg_products 
						left join devbg_categories on devbg_products.ItemCategory = devbg_categories.CategoryID
						left join devbg_subcategories on devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID 

						where devbg_products.ItemCategory = '$_GET[CategoryID]'

						order by ItemName limit $Start, $ByPage";

$qnav = "select devbg_products.*, devbg_categories.*, devbg_subcategories.* from devbg_products 
						left join devbg_categories on devbg_products.ItemCategory = devbg_categories.CategoryID
						left join devbg_subcategories on devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID 

						where devbg_products.ItemCategory = '$_GET[CategoryID]' ";
}
else
{

$q1 = "select devbg_products.*, devbg_categories.*, devbg_subcategories.* from devbg_products 
						left join devbg_categories on devbg_products.ItemCategory = devbg_categories.CategoryID
						left join devbg_subcategories on devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID 

						where devbg_products.ItemCategory = '$_GET[CategoryID]' and 
								  devbg_products.ItemSubcategory = '$_GET[subcategoryID]'

						order by ItemName limit $Start, $ByPage";

$qnav = "select devbg_products.*, devbg_categories.*, devbg_subcategories.* from devbg_products 
						left join devbg_categories on devbg_products.ItemCategory = devbg_categories.CategoryID
						left join devbg_subcategories on devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID 

						where devbg_products.ItemCategory = '$_GET[CategoryID]' and 
							  devbg_products.ItemSubcategory = '$_GET[subcategoryID]' ";
}



$r1 = mysql_query($q1) or die(mysql_error());

if(mysql_num_rows($r1) == '0')
{
$NoItems = "<center>There are no items at this category!</center>";
}
else
{


while($a1 = mysql_fetch_array($r1))
{
$MyPrice = number_format($a1[itemPrice], 2, ".", "");

if(!empty($a1[itemImage]))
{
	$xy = getimagesize("items_images/$a1[itemImage]");
	$x = $xy[0];
	$y = $xy[1];

	$NewItemName = str_replace("'", "", $a1[itemName]);

	$DisplayImage = "<a class=\"ItemTitle\" href=\"ViewItem.php?CategoryID=$a1[itemCategory]&ItemID=$a1[itemID]\"><img src=\"items_images/$a1[itemImage]\" width=100 alt=\"$a1[itemName]\" border=0 style=\"border-color:black\"></a>";
}
else
{
	$DisplayImage = " ";
}

$DisplayCategory .= "
<table align=center width=300 border=0 rules=rows cellspacing=0 bordercolor=black>
<tr>
	<td valign=top width=140>$DisplayImage</td>

	<td width=160 valign=top>
			<a class=\"ItemTitle\" href=\"ViewItem.php?CategoryID=$a1[itemCategory]&ItemID=$a1[itemID]\">$a1[itemName]</a><BR>
				<br>
			<font color=black face=verdana size=1><b>Category: </b></font><a class=\"CategoryClass\" href=\"ShowCategory.php?CategoryID=$a1[itemCategory]\">$a1[CategoryName]</a>";

	if(!empty($a1[subcategoryID]))
	{
		$DisplayCategory .= "<br><font color=black face=verdana size=1><b>Subcategory: </b></font><a class=\"SubcategoryClass\" href=\"ShowCategory.php?CategoryID=$a1[itemCategory]&SubcategoryID=$a1[itemSubcategory]\">$a1[subcategoryName]</a>";
	}

	$DisplayCategory .= "
        <br>
                <font color=red face=verdana size=2><b>Color: $a1[Color]</b></font>
                  <br>
                  <font color=red face=verdana size=2><b>Size: $a1[size]</b></font>
                  <br>
                <font color=red face=verdana size=2><b>Price: $$MyPrice</b></font>


			<br><br>

			<form method=post action=add.php style=\"margin:0\">
				<input type=hidden name=ItemID value=\"$a1[itemID]\">
				<input type=hidden name=ItemName value=\"$a1[itemName]\">
				<input type=hidden name=ItemShipping value=\"$a1[itemShipping]\">
				<input type=hidden name=ItemPrice value=\"$MyPrice\">
				<input type=submit name=s1 value=\"Add to card\" class=sub>

			</form>


		</td>					
</tr>

</table><br><hr size=1 color=E3E3E3>";


}


//build the "next" - "prev" navigatioin

$rnav = mysql_query($qnav) or die(mysql_error());
$rows = mysql_num_rows($rnav);

if($rows > $ByPage)
{
		$NextPrev =  "<br><table align=center width=600>";
		$NextPrev .= "<td align=center><font face=verdana size=2> | ";

		$pages = ceil($rows/$ByPage);

		for($i = 0; $i <= ($pages); $i++)
		{
			$PageStart = $ByPage*$i;

			$i2 = $i + 1;

			if($PageStart == $Start)
			{
				$links[] = " <span class=DNav>$i2</span>\n\t ";
			}
			elseif($PageStart < $rows)
			{
				$links[] = " <a class=Nav href=ShowCategory.php?Start=$PageStart&ByPage=$ByPage&CategoryID=$_GET[CategoryID]&SubcategoryID=$_GET[subcategoryID]>$i2</a>\n\t ";	
			}
		}

		$links2 = implode(" | ", $links);

		$NextPrev .= $links2;

		$NextPrev .=  "| </td>";

		$NextPrev .= "</table><br>\n";
}

}

if(isset($NoItems))
{
echo $NoItems;
}
else
{
require_once("templates/ShowCategoryTemplate.php");
}
require_once("footer.php");
?>

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

I cant use your code because its too large and messy to comprehend no offense, here is an example that might be more clear

 

 

  /// so lets start a new query
  $sql = mysql_query("...."); // dont feel like typing it in haha
  
  /// How many columns you want
  $columns = 2;

  /// Heres our counter thingy incrementer
  $i = 0;

  // Create the while loop
  while($row = mysql_fetch_array($sql)){
   
    if($i == 0){  print "<tr>"; // okay so open row if the $i hasnt reach 2 yet }  

    print "<td>".$row['somefield']."</td>"; // this is your data your printing off
    
    if($i == $columns){ print "</tr>";  // our $i has reach the maximum amount of columns, so close the row and start a new
  
    $i = 0; //reset the increment
    }
   $i++;
  }

Link to comment
Share on other sites

Also ADD this to the end of your script

 

if($i < $columns){    
for($j=$i; $j<$max_columns;$j++)        echo "<td> </td>";} ?>

 

or as a possible alternative would be

 

<?php   

$cols = 0;  
echo "<table><tr>";   

while ($cols < 20) {      

echo ($cols % 3 == 0)? "</tr><tr>" : "";     
echo "<td>$cols</td>";      $cols++;   

}   

echo "</tr></table>";?>

Link to comment
Share on other sites

i never wrote the code , and its ok , i try doing what u said i still cant get it to work the page dont show anything when i put the code where i things it gos :(

 

 

 


while($a1 = mysql_fetch_array($r1))
{
   if($i == 0){ if($i == $columns){
   $MyPrice = number_format($a1[itemPrice], 2, ".", "");
      
if(!empty($a1[itemImage]))
{
	$xy = getimagesize("items_images/$a1[itemImage]");
	$x = $xy[0];
	$y = $xy[1];

	$NewItemName = str_replace("'", "", $a1[itemName]);

$DisplayImage = "<a class=\"ItemTitle\" href=\"ViewItem.php?CategoryID=$a1[itemCategory]&ItemID=$a1[itemID]\"><img src=\"items_images/$a1[itemImage]\" width=100 alt=\"$a1[itemName]\" border=0 style=\"border-color:black\"></a>";
}
else
{
$DisplayImage = " ";
}

$DisplayCategory .= "
<table align=center width=300 border=0 rules=rows cellspacing=0 bordercolor=black>
<tr>
<td valign=top width=140>$DisplayImage</td>
<td width=160 valign=top>
<a class=\"ItemTitle\" href=\"ViewItem.php?CategoryID=$a1[itemCategory]&ItemID=$a1[itemID]\">$a1[itemName]</a><BR><br>
			<font color=black face=verdana size=1><b>Category: </b></font><a class=\"CategoryClass\" href=\"ShowCategory.php?CategoryID=$a1[itemCategory]\">$a1[CategoryName]</a>";

	if(!empty($a1[subcategoryID]))
	{
		$DisplayCategory .= "<br><font color=black face=verdana size=1><b>Subcategory: </b></font><a class=\"SubcategoryClass\" href=\"ShowCategory.php?CategoryID=$a1[itemCategory]&SubcategoryID=$a1[itemSubcategory]\">$a1[subcategoryName]</a>";
	}

	$DisplayCategory .= "
        <br>
                <font color=red face=verdana size=2><b>Color: $a1[Color]</b></font>
                  <br>
                  <font color=red face=verdana size=2><b>Size: $a1[size]</b></font>
                  <br>
                <font color=red face=verdana size=2><b>Price: $$MyPrice</b></font>


			<br><br>

			<form method=post action=add.php style=\"margin:0\">
				<input type=hidden name=ItemID value=\"$a1[itemID]\">
				<input type=hidden name=ItemName value=\"$a1[itemName]\">
				<input type=hidden name=ItemShipping value=\"$a1[itemShipping]\">
				<input type=hidden name=ItemPrice value=\"$MyPrice\">
				<input type=submit name=s1 value=\"Add to card\" class=sub>

			</form>


		</td>
</tr>

</table><br><hr size=1 color=E3E3E3>";


}
$i = 0; //reset the increment
}
$i++;

  }

Link to comment
Share on other sites

i never wrote the code , and its ok , i try doing what u said i still cant get it to work the page dont show anything when i put the code where i things it gos :(

 

 

 


while($a1 = mysql_fetch_array($r1))
{
   if($i == 0){ if($i == $columns){
   $MyPrice = number_format($a1[itemPrice], 2, ".", "");
      
if(!empty($a1[itemImage]))
{
	$xy = getimagesize("items_images/$a1[itemImage]");
	$x = $xy[0];
	$y = $xy[1];

	$NewItemName = str_replace("'", "", $a1[itemName]);

$DisplayImage = "<a class=\"ItemTitle\" href=\"ViewItem.php?CategoryID=$a1[itemCategory]&ItemID=$a1[itemID]\"><img src=\"items_images/$a1[itemImage]\" width=100 alt=\"$a1[itemName]\" border=0 style=\"border-color:black\"></a>";
}
else
{
$DisplayImage = " ";
}

$DisplayCategory .= "
<table align=center width=300 border=0 rules=rows cellspacing=0 bordercolor=black>
<tr>
<td valign=top width=140>$DisplayImage</td>
<td width=160 valign=top>
<a class=\"ItemTitle\" href=\"ViewItem.php?CategoryID=$a1[itemCategory]&ItemID=$a1[itemID]\">$a1[itemName]</a><BR><br>
			<font color=black face=verdana size=1><b>Category: </b></font><a class=\"CategoryClass\" href=\"ShowCategory.php?CategoryID=$a1[itemCategory]\">$a1[CategoryName]</a>";

	if(!empty($a1[subcategoryID]))
	{
		$DisplayCategory .= "<br><font color=black face=verdana size=1><b>Subcategory: </b></font><a class=\"SubcategoryClass\" href=\"ShowCategory.php?CategoryID=$a1[itemCategory]&SubcategoryID=$a1[itemSubcategory]\">$a1[subcategoryName]</a>";
	}

	$DisplayCategory .= "
        <br>
                <font color=red face=verdana size=2><b>Color: $a1[Color]</b></font>
                  <br>
                  <font color=red face=verdana size=2><b>Size: $a1[size]</b></font>
                  <br>
                <font color=red face=verdana size=2><b>Price: $$MyPrice</b></font>


			<br><br>

			<form method=post action=add.php style=\"margin:0\">
				<input type=hidden name=ItemID value=\"$a1[itemID]\">
				<input type=hidden name=ItemName value=\"$a1[itemName]\">
				<input type=hidden name=ItemShipping value=\"$a1[itemShipping]\">
				<input type=hidden name=ItemPrice value=\"$MyPrice\">
				<input type=submit name=s1 value=\"Add to card\" class=sub>

			</form>


		</td>
</tr>

</table><br><hr size=1 color=E3E3E3>";


}
$i = 0; //reset the increment
}
$i++;

  }

Sorry I forgot a closing brack after the IF($i==0){

 

 

Its not

 

  if($i == 0){ if($i == $columns){

 

 

But you didn't follow my example... Review your code and mine, and you'll what i mean.

 

 

Link to comment
Share on other sites

 

But you didn't follow my example... Review your code and mine, and you'll what i mean.

 

i try to follow ypur  example and i look over myay code and ur and i dont get it or understand it at all :( it my first time trying to edit this coding. do you know of some one who can fix it for a low cost?. again thank you for helpin

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.