how to make code in a right way not repeating?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
laura
Forum Newbie
Posts: 19
Joined: Wed May 17, 2006 1:06 pm

how to make code in a right way not repeating?

Post by laura »

hi,i am newbie to php.i am practising to use php to display products.
but i dun want the products to display vertically (list style). i wish the display would become horizontal.
is anyone has any ideas how to do it?thanks in advance.here is the code.hope it helps ^^v

Code: Select all

<html>
<head>
<title>livestock-shoes-adidas.php</title>
</head>

<body>
<?php 
include "IncludeFiles/db_config.php";
$myConn=mysql_connect($host,$uname,$pword);
$mydb=mysql_select_db($dbname,$myConn);
$sql="select * from products order by productid desc";
$rs=mysql_query($sql,$myConn);
$num_rows=mysql_num_rows($rs);
?>

<?php 
	for($i=1;$i<=$num_rows;$i++)
	{
	$row=mysql_fetch_array($rs);
	echo "<table border=1>";
	echo "<tr>";
	echo "<td>";
	echo "<b>".$row["pname"]."</b>";
	echo "</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td>";
	echo "<img src='images/shoes/adidas/".$row["pimg"]."' height=100 width=120>"."";
	echo "</td>";
	echo "</tr>";
	echo "</table>";
	}
	mysql_close($myConn);
?>
</body>
</html>
Last edited by laura on Sat Aug 12, 2006 5:06 am, edited 1 time in total.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

hum..
have you tried opening a <tr> before the loop. then doing <td> product </td> loop that forever.then close the </tr> after the loop?
User avatar
laura
Forum Newbie
Posts: 19
Joined: Wed May 17, 2006 1:06 pm

Post by laura »

thank you very much^^
i had put the table outside the looping.
but it is still not workingT_T
i can only display vertically but not horizontally.
i posted a pic,wish anyone who knows could help me display horizontally ^^
thanks in advance!!!!!! ^^v
Image
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Use a list:

Code: Select all

<style type="text/css"><!--
ul.products {
    float:left;
    margin:0;
    padding:0;
    width:600px; /* you may remove this rule to fill page */
}
ul.products li {
    float:left;
    display:block;
    margin:4px;
    border:solid #000 1px;
    padding:0;
}
--></style>
<ul class="products">
    <li><!-- product --></li>
    <!-- ad infinitium -->
</ul>
Much better markup, a list of products is not tabulated data and therfore should not be in a table. This will also display horizonally.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I've heard a couple of people say somethings not tabular data, I agree in the most part that some things that are not meant to be in tables shouldn't be (layouts for example), but it gets me wondering, can you define what is and what is not tabular data? I think it's opinion based.

In this example I disagree; I've never seen a list (non-computer related) which lists items horizontally. Also, doing this in tables instead of with CSS means that it can be pure mark-up, rather than making a CSS class for the lists which forces it horizontal - possibly causing cross-browser issues.

Here is how you could achieve your example with a table:

Code: Select all

<?php

$rowmax = 3; //Define how many items you want to show per table row

$images = //Set the array of data to be shown
array(
	'image1.jpg',
	'image2.jpg',
	'image3.jpg',
	'image4.jpg',
	'image5.jpg',
	'image6.jpg',
	'image7.jpg',
	'image8.jpg',
	'image9.jpg'
);

echo '<table>'; //Start the table
for($i = 0; $i <= count($images)-1; $i++) { //Loop through every item in the array
	if ($i % $rowmax == 0) echo '<tr>';
	echo '<td>'.$images[$i].'</td>'; //Show the item in the column
	if ($i % $rowmax == $rowmax - 1) echo '</tr>';
}
echo '</table>'; //End the table

?>
Last edited by jayshields on Wed Aug 09, 2006 9:07 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

take a look at the first link in Useful Posts.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

You may want to look at the modulus operator (%)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

jayshields wrote:In this example I disagree; I've never seen a list (non-computer related) which lists items horizontally.
Using a list allows a fluid layout, something that certainly cannot be said for tables.
User avatar
laura
Forum Newbie
Posts: 19
Joined: Wed May 17, 2006 1:06 pm

Post by laura »

THANK YOU EVERYONE!!!!!!!!!!!!
I FINALLY GOT IT DONE!!!!!!!
THANKS A THOUDSAND!!!!!! ^^V

Code: Select all

<?php 
include "IncludeFiles/db_config.php";
$myConn=mysql_connect($host,$uname,$pword);
$mydb=mysql_select_db($dbname,$myConn);
$sql="select m.productid,m.pname,m.pimg,c.brandid from products as m inner join brands as c on c.brandid=m.brandid where c.brandid=1 order by m.productid desc";
$rs=mysql_query($sql,$myConn);
$num_rows=mysql_num_rows($rs);

	$rowmax=3;	
	echo "<table border=1>";
	for($i=0;$i<=$num_rows-1;$i++)
	{
	$row=mysql_fetch_array($rs);
	if($i % $rowmax ==0)
	echo "<tr>";
	echo "<td align=center>";
	echo "<font face='verdana' size='-2'>".$row["pname"]."</font>";
	echo "<br>";
	echo "<a href='pdisplay.php?cat=".$row['productid']."'>";
	echo "<img src='images/shoes/adidas/".$row["pimg"]."' height=100 width=120>";
	echo "</a>";
	echo "</td>";
	if ($i % $rowmax == $rowmax-1)
	echo "<tr>";
	}
	echo "</table>";
	mysql_close($myConn);
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Just for clean coding sake:

Code: Select all

<?php
include 'IncludeFiles/db_config.php';
if (!$myConn = mysql_connect($host,$uname,$pword) )
{
    die('Could not connect to the DB server ' . $host . ': ' . mysql_error());
}

if ( !$mydb = mysql_select_db($dbname,$myConn) )
{
    die('Could not select the database ' . $dbname . ': ' . mysql_error());
}

$sql = 'SELECT m.`productid`, m.`pname`, m.`pimg`, c.`brandid` 
        FROM `products` AS m INNER JOIN `brands` as c 
            ON c.`brandid` = m.`brandid` 
        WHERE c.`brandid` = 1 
        ORDER BY m.`productid` DESC';
if ( !$rs = mysql_query($sql,$myConn) )
{
    die('Could execute the query <pre>' . $sql . '</pre> because: ' . mysql_error());
}

$num_rows = mysql_num_rows($rs);

$rowmax=3;     
echo '<table border="1">';
for ( $i = 0; $i <= $num_rows - 1 ; $i  )
{
    $row = mysql_fetch_array($rs);
    if ( $i % $rowmax ==0 )
        echo '<tr>';
    echo '<td align="center"><font face="verdana" size="-2">'.$row['pname'].'</font><br />';
    echo '<a href="pdisplay.php?cat='.$row['productid'].'"><img src="images/shoes/adidas/'.$row['pimg'].'" height="100" width="120"></a>';
    echo '</td>';
    if ( $i % $rowmax == $rowmax - 1 )
        echo '<tr>';
}
echo '</table>';
mysql_close($myConn);
?>
User avatar
laura
Forum Newbie
Posts: 19
Joined: Wed May 17, 2006 1:06 pm

Post by laura »

thanks for cleaning the code ^^v
i have question about stuffs like that..
actually i only make simple things.
i keep using the repeating code.but it works fine for me.but somehow i feel it is so strange.bcos
eg the

Code: Select all

include "IncludeFiles/db_config.php";
$myConn=mysql_connect($host,$uname,$pword);
$mydb=mysql_select_db($dbname,$myConn);
each time i use this in every php code.but it works fine.no errors.sometimes i try to make sql,sql2 in the code.but i screwed up.
but if i repeated the whole code.it is fine.here are some examples(no errors):

Code: Select all

<?php session_start(); ?>
<html>
<head>
<title>livestock-brands</title>
</head>
<style>
	a
	{
	text-decoration:none;
	}
</style>

<body>
<table align="center" width="750" height="455" border="1" cellpadding="0" cellspacing="0">
<tr>
	<td valign="top" colspan="3" height="90"><img src="images/logo.gif"></td>
	<td valign="top" align="right" colspan="3">address</td>
					
</tr>
<tr>
	<td height="20" width="125"><font size="-4"><a href="home.php">home</a></font></td>
	<td width="125">
	<?php 
	include "IncludeFiles/db_config.php";
	$myConn=mysql_connect($host,$uname,$pword);
	$mydb=mysql_select_db($dbname,$myConn);
	$sql="select * from categories";
	$rs=mysql_query($sql,$myConn);
	$num_rows=mysql_num_rows($rs);
	for($i=1;$i<=$num_rows;$i++)
	{
	$row=mysql_fetch_array($rs);
	echo "<font size=-4>";
	echo "<a href='brands.php?cid=".$row['categoryid']."'>";
	echo $row['cname']; 
	echo "</a>";
	echo "</font>";
	}
	mysql_close($myConn);
	?>
	</td>
	<td width="125">&nbsp;</td>
	<td width="125">&nbsp;</td>
	<td width="125">&nbsp;</td>
	<td width="125">&nbsp;</td>
</tr>
<tr>
	<td valign="middle" align="center" colspan="6" height="330">
	<?php 
	include "IncludeFiles/db_config.php";
	$myConn=mysql_connect($host,$uname,$pword);
	$mydb=mysql_select_db($dbname,$myConn);
	if(isset($_GET["cid"]))
	{
	$_SESSION["l"]=$_GET["cid"];
	$sql="select m.brandid,m.bname,m.blogoimg from brands as m inner join categories as c on c.categoryid=m.categoryid where c.categoryid=".$_GET["cid"];
	}
	else
	{
	$sql="select m.brandid,m.bname,m.blogoimg from brands as m inner join categories as c on c.categoryid=m.categoryid where c.categoryid=".$_SESSION["l"];
	}
	$rs=mysql_query($sql,$myConn);
	$num_rows=mysql_num_rows($rs);

	$rowmax=5;
	echo "<table>"; 
	for($i=0;$i<=$num_rows-1;$i++)
	{
	$row=mysql_fetch_array($rs);
	if($i % $rowmax ==0)
	echo "<tr>";
	echo "<td align=center>";
	echo "<font face=verdana size=-1>".$row["bname"]."</font>"."<br>";
	echo "<a href='bproducts.php?cat=".$row['brandid']."'>";
	echo "<img src='images/logos/".$row["blogoimg"]."' height=100 width=120><br><br>";
	echo "</a>";
	echo "</td>";
	if ($i % $rowmax == $rowmax-1)
	echo "</tr>";
	}
	echo "</table>";
	mysql_close($myConn);
	?>
	</td>
</tr>
<tr>
	<td align="center" colspan="6" height="15"></td>
</tr>
</table>

</body>
</html>

Code: Select all

<?php session_start(); ?>
<html>
<head>
<title>livestock-brand-products</title>
</head>
<style>
	a
	{
	text-decoration:none;
	}
</style>
<body>
<table align="center" width="750" height="455" border="1" cellpadding="0" cellspacing="0">
<tr>
	<td valign="top" colspan="3" height="90"><img src="images/logo.gif"></td>
	<td valign="top" align="right" colspan="3">address</td>
					
</tr>
<tr>
	<td height="20" width="125"><font size="-4"><a href="home.php">home</a></font></td>
	<td width="125">
	<?php 
	include "IncludeFiles/db_config.php";
	$myConn=mysql_connect($host,$uname,$pword);
	$mydb=mysql_select_db($dbname,$myConn);
	$sql="select * from categories";
	$rs=mysql_query($sql,$myConn);
	$num_rows=mysql_num_rows($rs);
	for($i=1;$i<=$num_rows;$i++)
	{
	$row=mysql_fetch_array($rs);
	echo "<font size=-4>";
	echo "<a href='brands.php?cid=".$row['categoryid']."'>";
	echo $row['cname']; 
	echo "</a>";
	echo "</font>";
	}
	mysql_close($myConn);
	?>
	</td>
	<td width="125">&nbsp;</td>
	<td width="125">&nbsp;</td>
	<td width="125">&nbsp;</td>
	<td width="125">&nbsp;</td>
</tr>
<tr>
	<td align="center" colspan="6" height="330">
	<?php 
	include "IncludeFiles/db_config.php";
	$myConn=mysql_connect($host,$uname,$pword);
	$mydb=mysql_select_db($dbname,$myConn);
	if(isset($_GET["cat"]))
	{
	$_SESSION["c"]=$_GET["cat"];
	$sql="select m.productid,m.pname,m.pimg,c.brandid,c.bname from products as m inner join brands as c on c.brandid=m.brandid where c.brandid=".$_GET["cat"]." order by m.productid desc";
	}
	else
	{
	$sql="select m.productid,m.pname,m.pimg,c.brandid,c.bname from products as m inner join brands as c on c.brandid=m.brandid where c.brandid=".$_SESSION["c"]." order by m.productid desc";
	}
	$rs=mysql_query($sql,$myConn);
	$num_rows=mysql_num_rows($rs);
	
	$rowmax=3;	
	echo "<table width='720' border=1>";
	for($i=0;$i<=$num_rows-1;$i++)
	{
	$row=mysql_fetch_array($rs);
	if($i % $rowmax ==0)
	echo "<tr>";
	echo "<td align=center>";
	echo "<font face='verdana' size='-2'>".$row["pname"]."</font>";
	echo "<br>";
	echo "<a href='bdisplay.php?cat=".$row['productid']."'>";
	echo "<img src='images/products/".$row["bname"]."/".$row["pimg"]."' height=100 width=120>";
	echo "</a>";
	echo "</td>";
	if ($i % $rowmax == $rowmax-1)
	echo "<tr>";
	}
	echo "</table>";
	mysql_close($myConn);
	?>
	</td>
</tr>
<tr>
	<td align="center" colspan="6" height="15"></td>
</tr>
</table>
</body>
</html>
is it correct if i doing in this way?in future will this cause problem?cos i found this is easy to do.that's y i repeating it.
pls correct me or let me know which one is the right way.thank you very much for the helps ^^v
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

laura
Forum Newbie


Joined: 17 May 2006
Funny how the username 'laura' was only taken that recently. Goes to show how many girls we get on devnet. Shame.
User avatar
laura
Forum Newbie
Posts: 19
Joined: Wed May 17, 2006 1:06 pm

Post by laura »

huh?i dun get what you mean... =.="
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

ole wrote:
laura
Forum Newbie


Joined: 17 May 2006
Funny how the username 'laura' was only taken that recently. Goes to show how many girls we get on devnet. Shame.
How do you know Laura is a female?

While I agree that any man who claimed to be 'prettier' than most would qualify for a kick to the teeth...names, especially these days are so cross gender, it's sterotyping...to assume one is a female or male based solely on name... :P

I'm just kidding...I'm not that sensitive...

Thats programming for you...most women simply aren't interested in it...

Codeproject.com has had one or two, which became somewhat of a celebrity once their identity was discovered...and theirs a community with over 3 million members...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Image
Post Reply