[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need Format advice for PHP/SQL query

Posted on 2004-11-23
9
Medium Priority
?
228 Views
Last Modified: 2010-08-05
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);
?>
0
Comment
Question by:broadspoke
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 12

Accepted Solution

by:
str_kani earned 200 total points
ID: 12656006
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
 

Author Comment

by:broadspoke
ID: 12656273
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
 
LVL 12

Expert Comment

by:str_kani
ID: 12656345
try this...

<td style="font-size:10px"></td>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:broadspoke
ID: 12656468
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
 
LVL 9

Expert Comment

by:gruntar
ID: 12656569
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
 
LVL 4

Expert Comment

by:bljak
ID: 12659075
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
 
LVL 2

Expert Comment

by:suresh_asp
ID: 12662875
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
 
LVL 9

Expert Comment

by:_GeG_
ID: 12662890
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
 
LVL 4

Expert Comment

by:bljak
ID: 12662936
hey cool one, but the last line is so awful :)

//bljak
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

826 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question