Problem with displaying mysql info

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

Problem with displaying mysql info

Post 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.
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Post 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.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.
Last edited by Bill H on Thu Dec 02, 2004 9:10 am, edited 1 time in total.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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.
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

Post 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?
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Check out nl2br() function. The carriage returns are being stored in the db, but html is ignoring them.
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

Post by Wldrumstcs »

Sorry, Im a newb... Could you show me what the script would look like witht he nl2br function? Thanks a ton!!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

Post by Wldrumstcs »

I'm very familiar with that site, but I couldn't figure how to incorporate that into my script.
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

Post by Wldrumstcs »

*bump*
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.
Post Reply