broadspoke
asked on
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);
?>
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",
$result = mysql_select_db($dblocal,$
$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);
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try this...
<td style="font-size:10px"></t d>
<td style="font-size:10px"></t
ASKER
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;
$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;
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;
$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;
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"]}</t d>\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
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\"><
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
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> &nb sp<a href='?prev=".($current-1) ."'>Prev</ a></td></t r>";
$list .= "</table>";
echo $list;
?>
<?php
$user = "****";
$pass = "****";
$table = "****";
$dblocal = "****";
$conn = mysql_connect("localhost",
$result = mysql_select_db($dblocal,$
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)
$list .= "</table>";
echo $list;
?>
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['Na me']}</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"
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['Na
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"
hey cool one, but the last line is so awful :)
//bljak
//bljak
ASKER