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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a better way to produce a line of links from db

Status
Not open for further replies.

leeboycymru

Programmer
Jan 23, 2007
185
GB
I have a piece of code that pulls out data from a databse and I want it to align horizontally until it reaches end of table and then it simply wraps around to start a new line.

The code im using is using a new UL and LI for each indivudual data, and seems very wasteful to me.

Can anybody help trim this code so that all it does is pull the data out and lets it run until its finished running horizontally but forming a vertical column due to width constraints.

Take a look at the link below, its the yellow output after 'Tour Operators':

<?php
$cnt = 0;
echo "<td>";
while($cnt < $top_count) {
$qw=mysql_query("select * from tbl_touroperators where Nom_Top = '$top_array[$cnt]'") or die (mysql_error());
$rw=mysql_fetch_assoc($qw);
echo "<ul class='navlist'>";
echo "<li class=\"middletext\"><a href=\" . $rw[Web_Top] . "\" class=\"bigweblinks\">" . $top_array[$cnt] . "</a></li>\n";
echo ($cnt % 1 === 0 || $cnt === $top_count - 1) ? "</ul>\n" : "";
$cnt = $cnt+1;
}
echo "</td>";
?>

And here is the link:


Cheers

Lee
 
get rid of the ul and li's and replace with a single span. give the span a distinct class and style it as you wish.

i really don't like the idea of a sql call for each number, either. is it really necessary? why don't you compile a list of id's you want to retrieve and include them in an IN where condition?
 
Hi there, this is a very old piece of code, and basically as a non PHP user, I have been given the task of sortign this out so that the output tiles horizontally forming a vertical column. I have got it so far b the above method, but i dont like it.

But your suggestion does make sense, but how i go about doing that I havent a clue at the moment.

lee
 
I tried it with a span instead of ul and li's and it did work but what happened was and its the same as using those tags is that it doesnt realise that if a link has 2 parts and it doesnt fit next to each other on one line then it doesnt push it to the next line. it just wraps the 2 words around, one on one line the other on the next as one link.
 
change the string in $top_array[$cnt] so that all spaces are replaces with non-breaking spaces (&nbsp;).

Code:
str_replace(" ", "&nbsp;",$top_array[$cnt]);

you might also be able to do something with the white-space delcaration in css.

add the following to the span class but remember to add a breakable space between the spans otherwise there will never be any wrapping. you could also force a width through the use of a div wrapping the spans.

Code:
white-space: nowrap;

we have left php however. this latter solution is a pure html css exercise so if you want to pursue this route and have futher questions, i would recomment posting in the html Forum 215
 
But still, we should go back to:

1. Using the list (because it is a list)
2. Not making a list for every link, but for all of them together.

So change your php to read (pseudo sql):
Code:
$sql = ("SELECT All applicable records FROM tables");
$qw = mysql_query ($sql) or die (mysql_error());
if (mysql_num_rows ($qw) != 0)
{
  echo '<ul class="navlist">';
  while ($rw = mysql_fetch_assoc ($qw))
  {
    echo '<li><a href="[URL unfurl="true"]http://'[/URL] . $rw['Web_Top'] .  '">' . $top_array[$cnt] . '</a></li>' . "\n";
  }
  echo '</ul>'; 
}
Then come back to HTML&CSS forum and we will help you style that accordingly.
 
thanks for that, just to clarify do i change just this piece of code:

$top_array[$cnt]

select it all and change it with this:

str_replace(" ", "&nbsp;",$top_array[$cnt]);

Because I did it, and it created an error
 
Hi Vragabond,

Ok, I put this together:

<?php
$cnt = 0;
$sql = ("select * from tbl_touroperators where Nom_Top = '$top_array[$cnt]'");
$qw = mysql_query ($sql) or die (mysql_error());
if (mysql_num_rows ($qw) != 0)
{
echo '<ul class="navlist">';
while ($rw = mysql_fetch_assoc ($qw))
{
echo '<li><a href=" . $rw['Web_Top'] . '">' . $top_array[$cnt] . '</a></li>' . "\n";
}
echo '</ul>';
}
?>


It doesnt produce any errors, but I have noticed that as $cnt = 0; it is only bringing out from the database the data in the 0 position, if i change it to 8 then it brings that one out.

Am I on the right track here?

lee
 
to stop line breaks programmatically change this line
Code:
echo '<li><a [red]class="linklist"[/red] href="[URL unfurl="true"]http://'[/URL] . $rw['Web_Top'] .  '">' . [red]str_replace(" ", "&nbsp;",$top_array[$cnt]) [/red]. '</a></li>' . "\n";

but it's neater to use css, imo (you should apply a class to the anchor too)

to get the whole of your list you will need to create an outer loop on $cnt and move the UL declarations outside of that loop

Code:
echo '<ul class="navlist">';
foreach ($top_array as $key=>$val){  //outer iteration
$sql = ("SELECT All applicable records FROM tables WHERE top_array='$key'");
$qw = mysql_query ($sql) or die (mysql_error());
if (mysql_num_rows ($qw) != 0)
{
    while ($rw = mysql_fetch_assoc ($qw))
  {
    echo '<li><a href="[URL unfurl="true"]http://'[/URL] . $rw['Web_Top'] .  '">' . str_replace(" ", "&nbsp;", $val) . '</a></li>' . "\n";
  }
  
}
}
echo '</ul>';
 
Your query, as is, will produce one result only. You will need to rewrite the query, so that the query will actually pull all the applicable records. We cannot write the query for you, because we do not know the criteria by which you're filtering database records, nor your database schema. But more or less, you should do the following.

1. Discover what your filter is.
2. Apply that filter to the query and pull out all the applicable records.
3. Send the html I showed you in my example to output the records into the list. You really do not need anything else to appropriately style that list and adding more classes will probably just clutter the html.
 
Wow, this is a bit scary now. I thought one day i would pick the PHP book up and start from the start, but this is taking the biscuit...

I'm trying my best to keep up.

lee
 
look, your criteria are held in an array called $top_array. this much can be derived from your earlier post.
Code:
<?php
$where = "IN ('".implode("','", $top_array) . "')";

$qw=mysql_query("select * from tbl_touroperators where Nom_Top $where") 
	or die (mysql_error());
echo "<ul class='navlist'>";
while ($row=mysql_fetch_assoc($qw)){
	echo <<<EOL
	<li class="middletext">
		<a 		href="[URL unfurl="true"]http://{$row[/URL]['Web_Top']}" 
				class="bigweblinks"	>
				{$rw['Nom_top']} 
		</a>
	</li>

EOL;
}
echo "</ul>";
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top