0

Thank you for answering my question at Ignore null results in MySQL JOIN queries

I've created my own radio station website in localhost, at http://myradiostation.localhost

This is the code:

<?php
    $connection = mysql_connect("localhost", "root", "PASSWORD") or die("Error connecting to database");
    mysql_select_db("radio1", $connection);
    $result = mysql_query("SELECT * FROM presenters;", $connection) or die("error querying database");
    $i = 0;
    while($result_ar = mysql_fetch_assoc($result)){
    ?>

This is the HTML code:

            <div class="divider"></div>
            <div class="main" style="width:552px;">
                <img src="<?php echo $result_ar['image']; ?>" width=115 height=60>
                <div class="time"><?php echo $result_ar['airtime']; ?></div>
                <div class="show"><h3><b><a href="<?php echo $result_ar['link']; ?>"><?php echo $result_ar['presenter']; ?></a></b></h3>
                    <p><?php echo $result_ar['showinfo']; ?></p></div>
                <div class="footer"></div>
            </div>
    <?php
    $i+=1;
    }
    ?>

It does work, except for one thing - the content without links still links back to the page itself, even though the database columns have some blank content.

Here's the SQL code - create a database called radio1 in PHPmyadmin and this code:

CREATE TABLE IF NOT EXISTS `presenters` (
  `presenter` varchar(255) NOT NULL,
  `link` varchar(255) NOT NULL,
  `image` varchar(255) NOT NULL,
  `airtime` time NOT NULL,
  `showinfo` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `presenters`
--

INSERT INTO `presenters` (`presenter`, `link`, `image`, `airtime`, `showinfo`) VALUES
('Non-Stop Nightshift', '', '', '01:00:00', 'More Music Variety through your weekend'),
('Greatest Hits', '', '', '06:00:00', 'Hit Music now!'),
('John Doe', 'http://www.myradiostation.localhost/johndoe', '', '08:00:00', 'Join John at the weekend');

It works with no major issues, except the linking one.

It displays properly, like this:

https://i.sstatic.net/b91Dj.jpg

How can I fix this? (if there's no images I suppose I could set a default value in the field though), and what would you recommend?

In short, how should I store HTML links in a PHPMyadmin database?

Thanks

2
  • Is that the entirety of your code? Where do your anchor (<a>) tags begin? Commented Jul 15, 2011 at 19:37
  • <div class="show"><h3><b><a href="<?php echo $result_ar['link']; ?>"><?php echo $result_ar['presenter']; ?></a></b></h3> is the code with the anchor tags. Commented Jul 15, 2011 at 19:40

2 Answers 2

0

Its not a PHP or MySQL problem. It's HTML 101. Whenever there is an <a href=""> tag, it will link to that page if it's blank. I believe that this will fix it: <php> If $array['link '] echo '<a>' You can make it a bit neater, but this is the basic idea.

Sign up to request clarification or add additional context in comments.

Comments

0

You will need to use a conditional statement to detect rows without links. Something like this:

<div class="show"><h3><b>
<?php
if ( empty( $result_ar['link'] ) ) {
    echo $result_ar['presenter'];
}
else {
    echo '<a href="' . $result_ar['link'] . '">' . $result_ar['presenter'] . '</a>';
}
?>
</b></h3>

8 Comments

Thanks! Where should I replace the code within the original coding then?
@avenas8808: You can replace the line <div class="show"><h3><b><a href="<?php echo $result_ar['link']; ?>"><?php echo $result_ar['presenter']; ?></a></b></h3> with the code above.
Tried that; got this error: Parse error: syntax error, unexpected '{' in C:\www\vhosts\localradio\schedsun.php on line 13
There was a missing right-parenthesis. I updated the answer. More importantly, do you understand what I did here? If the link field in the DB is empty, we are just outputing the name. Otherwise, we construct and output a link.
I think I do: on line 18: there is an extra double quote after the closing > for the link tag ($result_ar['link'] . '">"' ) should be ( $result_ar['link'] . '">' )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.