Page 1 of 1

Problem with displaying mysql info

Posted: Wed Dec 01, 2004 5:34 pm
by Wldrumstcs
Ok, I have a bio page that finds variables stored in a mysql db. One of the fields is called 'links' and has multiple things posted into it using paragraphs to separate each one. I want the displayed links to be separated by a paragraph (or a break). Here is my code:

Code: Select all

<?
mysql_connect("localhost","$username","$password") or die ("Unable to connect to MySQL server."); 
$db = mysql_select_db("$database") or die ("Unable to select requested database.");

$query="SELECT links FROM teachers WHERE id='$idcheck'";
$result=mysql_query($query);

$num=mysql_numrows($result);

$i=0;
while ($i < $num) {
$linksshow=mysql_result($result,$i,"links");

	echo "
			<tr>
				<td width='100%'>
				<p align='center'><a href='$linksshow'>$linksshow</a></p></td>
			</tr>
			";
			$i++;
}

?>
I tried displaying them a different way, but instead of being separated by a paragraph it is separated by a single space. Should I change the way the links are stored in the DB field or can I fix the code to make it work.

Posted: Thu Dec 02, 2004 2:37 am
by thomas777neo
Ok, on line 8 of your code. The command is mysql_num_rows not mysql_numrows.

It look like you are trying to display the data in a table. Where are the required table tags?

Anyway, to make it easy, you could probably do something like this:

Code: Select all

echo "<a href="$linksshow">$linksshow</a><br>";
This will then display your links as follows:

link1
link2
link3

or you can simple say this:

Code: Select all

echo "<p><a href="$linksshow">$linksshow</a></p>";
I think you trouble lies in the HTML not in your DB.

Posted: Thu Dec 02, 2004 9:06 am
by Bill H
You can't use a <p> tag within a <t> tag -- the browser will ignore it.
If you want more space use multiple spaces, which you must do the hard way,
by using the ampersand and nbsp multiple times.
For separate libes use <br> as Thomas suggested.
And I agree with him, the problem isn't in your bd.

Posted: Thu Dec 02, 2004 9:09 am
by Dale

Code: Select all

<tr>
                <td width='100%' align='center'>
                <a href='$linksshow'>$linksshow</a><br /><br />
                </td>
            </tr>
As Bill H mentioned, you cannot use <p> tags inside <tr>, <td> tags. Above is what should work for that bit.

Posted: Thu Dec 02, 2004 4:17 pm
by Wldrumstcs
My table tags lie outside the above PHP. I took out the <p> tags and fixed what you told me to fix, but it still doesn't work. I think it may be because of how the links are stored in the DB. The person entering them just types one link, then presses "Enter", types the next link, presses "Enter", etc... and then that info is stored into the DB. If I use the code I have written, will it display it the way I want it to?

Posted: Thu Dec 02, 2004 5:01 pm
by Bill H
Check out nl2br() function. The carriage returns are being stored in the db, but html is ignoring them.

Posted: Thu Dec 02, 2004 5:14 pm
by Wldrumstcs
Sorry, Im a newb... Could you show me what the script would look like witht he nl2br function? Thanks a ton!!

Posted: Thu Dec 02, 2004 5:28 pm
by timvw
well.... here is a tip...

just prepend http://www.php.net/ to the function you want info about

thus -> http://www.php.net/nl2br

Posted: Thu Dec 02, 2004 5:39 pm
by Wldrumstcs
I'm very familiar with that site, but I couldn't figure how to incorporate that into my script.

Posted: Fri Dec 03, 2004 5:31 pm
by Wldrumstcs
*bump*

Posted: Fri Dec 03, 2004 8:02 pm
by Bill H
Well, I think the documentation is pretty straightforward, but:

Code: Select all

<tr>
<td width='100%' align='center'>
<?php echo nl2br($linksshow) ?>
</td>
</tr>
You'll need to adapt that to make it become links, but I'm not going to do that much work for you.