Click to See Complete Forum and Search --> : converting query into html...
rch10007
08-17-2005, 08:53 AM
When I am echo'ing results from a query...alot of text, and i would like to place <p> tags within the text, how could i go about doing this.
is there some sort of identifer or something I can include in the text and create some code to replace these - nevermind (i might as well put the tags in myself if i do that).
what's the "easy" way of going about this? can sql read line breaks or something?
I am doing well with creating PHP code but my mysql skills are still noobish.
thx
ShrineDesigns
08-17-2005, 09:59 AM
it is really easy to do, example<?php
// ...
while($row= mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<p>{$row['title']}<br />
{$row['description']}</p>";
}
?>here is how i format links on my site, it is a good example of a fairly dynamic formattingfunction parse_links($link, $bnew = 0, $btop = 0, $bpop = 0)
{
global $lang, $ppg;
$cat = '';
$extra = '';
if(count($link) > 8)
{
$parent = parent_category($link['category']);
$child = current_category($link['category']);
if(!is_null($parent))
{
$cat = array("<a href=\"index.php?".URL_CAT."={$parent['id']}&".URL_PG."=1&".URL_PPG."={$ppg}\">{$parent['label']}</a>", "<a href=\"index.php?".URL_CAT."={$child['id']}&".URL_PG."=1&".URL_PPG."={$ppg}\">{$child['label']}</a>");
}
else
{
$cat = array("<a href=\"index.php?".URL_CAT."={$child['id']}&".URL_PG."=1&".URL_PPG."={$ppg}\">{$child['label']}</a>");
}
$cat = "\n <div class=\"category\">" . implode(' > ', $cat) . '</div>';
}
if($bnew && is_new($link['added']))
{
$extra .= "\n <b class=\"new\">" . $lang['New'] . "</b>";
}
if($btop && is_top($link['rank']))
{
$extra .= "\n <b class=\"top\">" . $lang['Top_Rated'] . "</b>";
}
if($bpop && is_pop($link['hits']))
{
$extra .= "\n <b class=\"pop\">" . $lang['Popular'] . "</b>";
}
$link['read'] = '';
if($link['reviews'] > 0)
{
$link['read'] = " | <a href=\"review.php?id={$link['id']}&read\">" . $lang['Read_Reviews'] . "</a>";
}
$link['added'] = fdate($link['added']);
$link['title'] = htmlspecialchars($link['title'], NULL, $lang['ENCODING']);
$link['description'] = wordwrap(htmlspecialchars($link['description'], NULL, $lang['ENCODING']), 76, "\n ", 0);
return " <p class=\"link\"><a href=\"goto.php?id={$link['id']}\">{$link['title']}</a>{$extra}{$cat}
<div class=\"description\">{$link['description']}</div>
<div class=\"stats\">[ {$lang['Added']}: {$link['added']} | {$lang['Hits']}: {$link['hits']} | {$lang['Votes']}: {$link['votes']} | {$lang['Rating']}: {$link['rank']} |
{$lang['Reviews']}: {$link['reviews']}{$link['read']} | <a href=\"review.php?id={$link['id']}&post\">{$lang['Post_Review']}</a> | <a href=\"rank.php?id={$link['id']}\">{$lang['Rate']}</a> | <a href=\"report.php?id={$link['id']}\">{$lang['Dead_Link']}</a> ]</div>
</p>
";
}
function parent_category($id)
{
global $category;
$sub = current_category($id);
foreach($category as $cat)
{
if($cat['id'] == $sub['sub'])
{
return $cat;
}
}
}
function current_category($id)
{
global $category;
foreach($category as $cat)
{
if($cat['id'] == $id)
{
return $cat;
}
}
}
function get_avg($field)
{
global $db;
$result = $db->query("SELECT AVG(`$field`) FROM `" . TBL_LINKS . "` WHERE `$field` != 0");
return $db->result($result, 0);
}
function is_top($stat)
{
static $avg = NULL;
if(is_null($avg))
{
$avg = get_avg('rank');
}
return ($stat >= $avg);
}
function is_pop($stat)
{
static $avg = NULL;
if(is_null($avg))
{
$avg = get_avg('hits');
}
return ($stat >= $avg);
}
function is_new($stat)
{
global $config;
static $avg = NULL;
if(is_null($avg))
{
$avg = mktime(0, 0, 0, NULL, 0 - $config['new_length']);
}
return ($stat >= $avg);
}
NogDog
08-17-2005, 10:09 AM
The quick and dirty way is to use nl2br() when you output the text data. (It replaces each newline with the HTML <br /> tag.)
A more valid HTML markup way would be something like:
$testString =<<<EOD
This is a test.
It is only a test.
The end.
EOD;
echo "<p>" . preg_replace("/(\n|\r\n)+/", "</p>\n<p>", trim($testString)) . "</p>";
rch10007
08-17-2005, 07:21 PM
Great answers!
thx...
rch10007
08-17-2005, 08:16 PM
NOGDOG
Just out of curiosity, I copied and pasted your code and it throws error on this line:
$testString =<<<EOD
if I remove the <<<EOD and EOD; - i then get errors on this line:
echo "<p>" . preg_replace("/(\n|\r\n)+/", "</p>\n<p>", trim($testString)) . "</p>";
In either case the errors are parse errors.
I replaced -- "/(\n|\r\n)+/" with -- "/\n|\r\n/" and it works.
not sure why the EOD throws the errors??
NogDog
08-17-2005, 10:49 PM
Make sure there were no spaces added by the copy-and-paste after the <<<EOD, and that the closing EOD; is at the very beginning of the line with no white-space in front of it.
rch10007
08-17-2005, 11:14 PM
I used this because the other way was adding an extra <p></p> inbetween sentences.
$patterns = array ( "/^/m", "/$/m", "/<p>\r<\/p>/" );
$replacements = array ( "<p>", "</p>", "");
echo preg_replace($patterns, $replacements, trim($testString));
Oh yea, NOGDOG - you were right - i checked for xtra whitespace and no it's fine that way - with <<<EOD, EOD;