Page 1 of 1

There has to be a better way......

Posted: Tue Apr 14, 2009 8:53 am
by nicademus
Hello to everyone!
I am very new to php and have written my first script, or at least found one and changed it a little to suit my needs. But after a little more research and reading I know there has to be a better way to do this. Essentially for each page I need to pull a single company's information form a few different tables in different areas of the page, but the script I am using is a loop looping through one company's info. I knwo I probably don't need the loop but am not sure where to start looking for a better solution. If someone could point me in the right direction I would be very grateful.
Here is the code I am using now:

Code: Select all

 
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM co_links WHERE co_name='Playful Promises'";
$result=mysql_query($query);
 
$num=mysql_num_rows($result); 
?>
 
<ul>
 
<?php
 
$i=0;
while ($i < $num) {
 
 
$link_1=mysql_result($result,$i,"link_1");
$text_1=mysql_result($result,$i,"text_1");
$link_2=mysql_result($result,$i,"link_2");
$text_2=mysql_result($result,$i,"text_2");
$link_3=mysql_result($result,$i,"link_3");
$text_3=mysql_result($result,$i,"text_3");
$link_4=mysql_result($result,$i,"link_4");
$text_4=mysql_result($result,$i,"text_4");
$link_5=mysql_result($result,$i,"link_5");
$text_5=mysql_result($result,$i,"text_5");
$link_6=mysql_result($result,$i,"link_6");
$text_6=mysql_result($result,$i,"text_6");
$link_7=mysql_result($result,$i,"link_7");
$text_7=mysql_result($result,$i,"text_7");
$link_8=mysql_result($result,$i,"link_8");
$text_8=mysql_result($result,$i,"text_8");
$link_9=mysql_result($result,$i,"link_9");
$text_9=mysql_result($result,$i,"text_9");
$link_10=mysql_result($result,$i,"link_10");
$text_10=mysql_result($result,$i,"text_10");
$link_11=mysql_result($result,$i,"link_11");
$text_11=mysql_result($result,$i,"text_11");
 
 ?>
 
 
 
<li><a href="<?php echo $link_1; ?>"><?php echo $text_1; ?></a></li>
<li><a href="<?php echo $link_2; ?>"><?php echo $text_2; ?></a></li>
<li><a href="<?php echo $link_3; ?>"><?php echo $text_3; ?></a></li>
<li><a href="<?php echo $link_4; ?>"><?php echo $text_4; ?></a></li>
<li><a href="<?php echo $link_5; ?>"><?php echo $text_5; ?></a></li>
<li><a href="<?php echo $link_6; ?>"><?php echo $text_6; ?></a></li>
<li><a href="<?php echo $link_7; ?>"><?php echo $text_7; ?></a></li>
<li><a href="<?php echo $link_8; ?>"><?php echo $text_8; ?></a></li>
<li><a href="<?php echo $link_9; ?>"><?php echo $text_9; ?></a></li>
<li><a href="<?php echo $link_10; ?>"><?php echo $text_10; ?></a></li>
<li><a href="<?php echo $link_11; ?>"><?php echo $text_11; ?></a></li>
 
</ul>
<?php
$i++;
}
 
?>
  </li>
 
Thank you in advance :)

If I have not posted anything correctly please let me know as I am still very new to posting in forums. Thanx!

Re: There has to be a better way......

Posted: Tue Apr 14, 2009 9:18 am
by requinix
It'll be so much easier if you normalize your tables.

Have one table that keeps track of one link/text for one company. But the table can have more than one entry. Like

Code: Select all

company | link  | text
--------+-------+------------
 1      | abc   | link to abc
 2      | bcd   | link to bcd
 1      | cde   | link to cde
 1      | def   | link to def
 3      | efg   | link to efg
 2      | fgh   | link to fgh
Then you run a query to get all the rows that belong to the company (...WHERE company=X). Loop through the results, print, and you're done.

Re: There has to be a better way......

Posted: Fri Apr 17, 2009 8:50 am
by nicademus
Awesome Thanx man!