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.stack.imgur.com/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

Community
  • 1
  • 1
avenas8808
  • 1,639
  • 3
  • 20
  • 33

2 Answers2

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>
George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • Thanks! Where should I replace the code within the original coding then? – avenas8808 Jul 15 '11 at 19:41
  • @avenas8808: You can replace the line `

    ` with the code above.
    – George Cummins Jul 15 '11 at 19:42
  • Tried that; got this error: **Parse error: syntax error, unexpected '{' in C:\www\vhosts\localradio\schedsun.php on line 13** – avenas8808 Jul 15 '11 at 19:43
  • 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. – George Cummins Jul 15 '11 at 19:44
  • It almost works, but got this error: 08:00:00 "John Doe - not sure why the quotation mark is still there though ? @George Cummins, I do understand, thanks for your help. – avenas8808 Jul 15 '11 at 19:47
  • There is either an extraneous quotation mark in the area where you pasted the code (perhaps around the `

    `) or the quotation mark is in the database row itself. If you want to append your new code to your answer, I can help you find it.

    – George Cummins Jul 15 '11 at 19:50
  • I see nothing in that code that would introduce the quotation mark. – George Cummins Jul 15 '11 at 20:04
  • 1
    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'] . '">'` ) – horatio Jul 15 '11 at 20:39
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.

Eric
  • 371
  • 2
  • 11