Displaying links in PHP/MySQL?

扶醉桌前 提交于 2020-01-06 08:57:08

问题


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:

http://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


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/6712204/displaying-links-in-php-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!