Need Format advice for PHP/SQL query

Hello,

Thanks in advance for the help.  I am really new to coding and need some advice on formating my query results.  The current query works well, but I want to show the results smaller then what is coming back   The list is getting to long and not manageable.

Thanks again for your advice.

<?php

$user = "****";
$pass = "****";
$table = "****";
$dblocal = "****";

$conn = mysql_connect("localhost", "$user", "$pass") or die ("Unable to Connect");

$result = mysql_select_db($dblocal,$conn) or die ("Err:db");

$query = "SELECT * FROM $table ORDER BY Name";

$result = mysql_query($query, $conn) or die ("Something wrong");;

$list = "<table border=\"0\" cellpadding=\"0\">";
$list.="<tr><th>Useful Links</th></tr>";
                                                 
while ($data = mysql_fetch_assoc($result))

{
     $list .= "<tr>";
       $list .= "<td><a href='" . $data['URL'] . "' target='_blank'>" . $data['Name'] . "</td>";
     $list .= "</tr>";
}
$list .= "</table>";

echo($list);
?>
broadspokeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

str_kaniCommented:
If you want to list all the items vertically you have to follow the same logic,
if it's ok for you to display them 4 or 5 per row you can make it smaller 4 or 5 times...


for this... keep a counter inside the while like $counter=5;


 $list = "<table>";
$counter=1;
while ($data = mysql_fetch_assoc($result))
{

if($counter==1)
     $list .= "<tr>";

if($counter==5)
{
  $list .= "</tr>";
  $counter=0;
}
      $list .= "<td><a href='" . $data['URL'] . "' target='_blank'>" . $data['Name'] . "</td>";
      $counter++;
}
$list .= "</table>";

echo $list;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
broadspokeAuthor Commented:
Thanks for the tip.  I use this DB for customer access to commonly used websites.  So what I really need is to shrink the font to a usable size.  I can't figure how to do this.
0
str_kaniCommented:
try this...

<td style="font-size:10px"></td>
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

broadspokeAuthor Commented:
I tried it like this below.  But it crashes.
 
$list = "<table>";
$counter=1;
while ($data = mysql_fetch_assoc($result))
{

if($counter==1)
     $list .= "<tr>";

if($counter==5)
{
  $list .= "</tr>";
  $counter=0;
}
      $list .= "<td style="font-size:10px"><a href='" . $data['URL'] . "' target='_blank'>" . $data['Name'] . "</td>";
      $counter++;
}
$list .= "</table>";

echo $list;
0
gruntarCommented:
Try this code

$list = "<table>";
$counter=1;
while ($data = mysql_fetch_assoc($result))
{

if($counter==1)
     $list .= "<tr>";

if($counter==5)
{
  $list .= "</tr>";
  $counter=0;
}
      $list .= '<td style="font-size:10px"><a href="' . $data['URL'] . '" target="_blank">' . $data['Name'] . '</td>';
      $counter++;
}
$list .= "</table>";

echo $list;


0
bljakCommented:
hm yea, nice, wasting memory basically what i always was for in EE is to explain people what are they doing wrong and not give them code without explaining wtf did happen now

your problem above was not to escape double quote within double quotes
ie. if you want out in html like this
  I am "very" nice.
you need to print have following php code
 print("I am \"very\"nice.");

because you started the string with double quotes you can not use those in string without escaping them
thatswhy gruntar put stuff in single quotes and mixed double qutes and so on and so on
my tip is to try to use always same quotes and put variables within string into brackets ie.

$list .= '<td style="font-size:10px"><a href="' . $data['URL'] . '" target="_blank">' . $data['Name'] . '</td>';

i do not think its nice formatted and readable and understoodable wtf does it do, thus with a normal client and syntax highlighting you will read code below better

$list .= "<td style=\"font-size:10px\"><a href=\"{$data["URL"]}\" target=\"_blank\">{$data["Name"]}</td>\r\n";

however you load the big nice table into memory wasting it, and basically you need it for output, why just not printing out on output device instead putting it in memory
in my opinion, doing print() or echo() each time you do $list .=
is more efficient and faster than loading pure html output into memory (unless ofc you need to output this elsewhere on the same page)
ah yes, try to add carriage return and new line where needed so you can read html source code better for debugging

//bljak
0
suresh_aspCommented:
try this,

<?php
$user = "****";
$pass = "****";
$table = "****";
$dblocal = "****";

$conn = mysql_connect("localhost", "$user", "$pass") or die ("Unable to Connect");

$result = mysql_select_db($dblocal,$conn) or die ("Err:db");

if (isset($_GET['next']))
      $current = $_GET['next'];
if (isset($_GET['prev']))
      $current = $_GET['prev'];

$lim = $current*5;

if(!isset($current))
      $query = "SELECT * FROM $table limit 0,5";
else
      $query = "SELECT * FROM $table limit ".$lim.",5";

$result = mysql_query($query, $conn) or die ("Something wrong");

$list = "<table>";

while ($data = mysql_fetch_assoc($result))
{
    $list .= "<tr>";

    $list .= "<td style='font-size:12px'><a href='" . $data['URL'] . "' target='_blank'>" . $data['Name'] . "</td>";
    $list .= "</tr>";
}
$list .= "<tr><td colspan=2 align=right style='font-size:12px'><a href='?next=".($current+1)."'>Next</a>&nbsp&nbsp<a href='?prev=".($current-1)."'>Prev</a></td></tr>";
$list .= "</table>";

echo $list;
?>
0
_GeG_Commented:
ok, here are my $0.02
please note the newlines for bljak ;)

<?php
$counter=-1;
echo '<table cellspacing="1" cellpadding="0">'."\n";
while ($data = mysql_fetch_assoc($result)){
    echo (++$counter%5 ? '' : ($counter ? "</tr>\n" : '').'<tr>').<<<EOS
<td style="font-size:10px"><a href="{$data['URL']}" target="_blank">{$data['Name']}</td>

EOS;
}
echo '</tr>'."\n".'</table>';
?>

if you want to know what happens:
<<< is called heredoc and is explained here: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
and the {} around a variable in a string are explained on the same page, look below the heredoc description for "Complex (curly) syntax"
0
bljakCommented:
hey cool one, but the last line is so awful :)

//bljak
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.