帮助格式化 PHP 中的新闻条目

发布于 2024-11-05 15:37:39 字数 4835 浏览 0 评论 0原文

我的网站上,用户可以登录并添加新闻。效果很好。我在尝试显示新闻时遇到问题。第一个问题是,如果新闻文本没有填充新闻图像旁边的所有空间,那么下一个新闻项目就会显示得太快(不在橙色断路器下方),正如您目前在网站上看到的那样。我想解决这个问题,我可以将每个新闻帖子 div 的高度设置为图像的高度,尽管图像比 div 短一点,所以我不确定如何做到这一点。

其次,用户在他们的新闻帖子中放置链接。如何让它们显示为活动状态?在 Firefox 上它们只是以文本形式出现。有人可以指出我正确的方向吗?

这是代码:

$query="SELECT id, date, title, text, author, media1, media2, deleted FROM news ORDER BY id DESC LIMIT 4";
$result=mysql_query($query);

$counter = 0;
$number1 = 1;
$number2 = 2;
while($row = mysql_fetch_array($result)){
        if($row['deleted'] == 0) {
                        if (($counter % 2) == 0) {
echo '<div id="text">';
echo '<a name="'.stripslashes($row['title']).'" id="'.stripslashes($row['title']).'"></a>';
echo '<span class="kisstitle">'.stripslashes($row['title']).'</span><br>';
echo ' (';
echo $row['date'];
echo ')';
echo '<br>'; 
echo '<br>'; 



if((preg_match ("/\bjpg\b/",$row['media1'])) || (preg_match ("/\bjpeg\b/",$row['media1'])) || (preg_match ("/\bpng\b/i",$row['media1'])) || (preg_match ("/\bgif\b/i",$row['media1']))){

echo '<img style="max-width:300px;" src="media/news/'.$row['media1'].'" class="floatRightClear" id="border">';

}

if((preg_match ("/\bjpg\b/",$row['media2'])) || (preg_match ("/\bjpeg\b/",$row['media2'])) || (preg_match ("/\bpng\b/i",$row['media2'])) || (preg_match ("/\bgif\b/i",$row['media2']))){

echo '<img style="max-width:300px;" src="media/news/'.$row['media2'].'" class="floatRightClear" id="border">';

}




if((preg_match ("/\bmp3\b/", $row['media1']))) {

    echo ' <p id="audioplayer_'.$number1.'" class="floatRightClear">Media Content</p>  
        <script type="text/javascript">  
        AudioPlayer.embed("audioplayer_'.$number1.'", {soundFile: "http://kiddiessupportscheme.org/media/news/'.$row['media1'].'"});  
        </script>';
        echo '<br>';
}

if((preg_match ("/\bmp3\b/", $row['media2']))) {

    echo ' <p id="audioplayer_'.$number2.'" class="floatRightClear">Media Content</p>  
        <script type="text/javascript">  
        AudioPlayer.embed("audioplayer_'.$number2.'", {soundFile: "http://kiddiessupportscheme.org/media/news/'.$row['media2'].'"});  
        </script>';
        echo '<br>';

}

echo stripslashes(nl2br($row['text']));
echo '<br><br>'; 
echo stripslashes($row['author']);
echo '</div>';

echo '<p align="right" id="seperater"><a href="index2.php?op=News#top">Top</a><img src="images/seperater.jpg" width="950" height="6" style="border:none;"  /></p>';
}

else {
echo '<div id="text">';
echo '<a name="'.stripslashes($row['title']).'" id="'.stripslashes($row['title']).'"></a>';
echo '<span class="kisstitle">'.stripslashes($row['title']).'</span><br>';
echo ' (';
echo $row['date'];
echo ')';
echo '<br>'; 
echo '<br>'; 


if((preg_match ("/\bjpg\b/",$row['media1'])) || (preg_match ("/\bjpeg\b/",$row['media1'])) || (preg_match ("/\bpng\b/i",$row['media1'])) || (preg_match ("/\bgif\b/i",$row['media1']))){

echo '<img style="max-width:300px;" src="media/news/'.$row['media1'].'" class="floatLeftClear" id="border">';

}

if((preg_match ("/\bjpg\b/",$row['media2'])) || (preg_match ("/\bjpeg\b/",$row['media2'])) || (preg_match ("/\bpng\b/i",$row['media2'])) || (preg_match ("/\bgif\b/i",$row['media2']))){

echo '<img style="max-width:300px;" src="media/news/'.$row['media2'].'" class="floatLeftClear" id="border">';

}



if((preg_match ("/\bmp3\b/", $row['media1']))) {

    echo ' <p id="audioplayer_'.$number1.'" class="floatLeftClear">Media Content</p>  
        <script type="text/javascript">  
        AudioPlayer.embed("audioplayer_'.$number1.'", {soundFile: "http://kiddiessupportscheme.org/media/news/'.$row['media1'].'"});  
        </script>';
        echo '<br>';

}
if((preg_match ("/\bmp3\b/", $row['media2']))) {

    echo ' <p id="audioplayer_'.$number2.'" class="floatLeftClear">Media Content</p>  
        <script type="text/javascript">  
        AudioPlayer.embed("audioplayer_'.$number2.'", {soundFile: "http://kiddiessupportscheme.org/media/news/'.$row['media2'].'"});  
        </script>';
        echo '<br>';

}

echo stripslashes(nl2br($row['text']));
echo '<br><br>'; 
echo stripslashes($row['author']);
echo '</div>';


echo '<p align="right" id="seperater"><a href="index2.php?op=News#top">Top</a><img src="images/seperater.jpg" width="950" height="6" style="border:none;"  /></p>';

}
$number1++;
$number1++;
$number1++;
$number2++;
$number2++;
$number2++;
$counter++;
}}

On My Site users can login and add news. That works fine. I have problems when trying to display the news. First problem is if the news text doesn't fill all the space by the side of the news image, then the next news item gets displayed too soon (not below the orange breaker) as you can see on the site at the moment. I was thinking to get around this I could set the height of each news post div to the height of the image, although the image is a little shorter than the div so I'm not sure how I'd do that.

Secondly, users put links in their news posts. How do I get them to be displayed as active? on firefox they just come out as text. Could someone point me in the right direction please!

Here's the code:

$query="SELECT id, date, title, text, author, media1, media2, deleted FROM news ORDER BY id DESC LIMIT 4";
$result=mysql_query($query);

$counter = 0;
$number1 = 1;
$number2 = 2;
while($row = mysql_fetch_array($result)){
        if($row['deleted'] == 0) {
                        if (($counter % 2) == 0) {
echo '<div id="text">';
echo '<a name="'.stripslashes($row['title']).'" id="'.stripslashes($row['title']).'"></a>';
echo '<span class="kisstitle">'.stripslashes($row['title']).'</span><br>';
echo ' (';
echo $row['date'];
echo ')';
echo '<br>'; 
echo '<br>'; 



if((preg_match ("/\bjpg\b/",$row['media1'])) || (preg_match ("/\bjpeg\b/",$row['media1'])) || (preg_match ("/\bpng\b/i",$row['media1'])) || (preg_match ("/\bgif\b/i",$row['media1']))){

echo '<img style="max-width:300px;" src="media/news/'.$row['media1'].'" class="floatRightClear" id="border">';

}

if((preg_match ("/\bjpg\b/",$row['media2'])) || (preg_match ("/\bjpeg\b/",$row['media2'])) || (preg_match ("/\bpng\b/i",$row['media2'])) || (preg_match ("/\bgif\b/i",$row['media2']))){

echo '<img style="max-width:300px;" src="media/news/'.$row['media2'].'" class="floatRightClear" id="border">';

}




if((preg_match ("/\bmp3\b/", $row['media1']))) {

    echo ' <p id="audioplayer_'.$number1.'" class="floatRightClear">Media Content</p>  
        <script type="text/javascript">  
        AudioPlayer.embed("audioplayer_'.$number1.'", {soundFile: "http://kiddiessupportscheme.org/media/news/'.$row['media1'].'"});  
        </script>';
        echo '<br>';
}

if((preg_match ("/\bmp3\b/", $row['media2']))) {

    echo ' <p id="audioplayer_'.$number2.'" class="floatRightClear">Media Content</p>  
        <script type="text/javascript">  
        AudioPlayer.embed("audioplayer_'.$number2.'", {soundFile: "http://kiddiessupportscheme.org/media/news/'.$row['media2'].'"});  
        </script>';
        echo '<br>';

}

echo stripslashes(nl2br($row['text']));
echo '<br><br>'; 
echo stripslashes($row['author']);
echo '</div>';

echo '<p align="right" id="seperater"><a href="index2.php?op=News#top">Top</a><img src="images/seperater.jpg" width="950" height="6" style="border:none;"  /></p>';
}

else {
echo '<div id="text">';
echo '<a name="'.stripslashes($row['title']).'" id="'.stripslashes($row['title']).'"></a>';
echo '<span class="kisstitle">'.stripslashes($row['title']).'</span><br>';
echo ' (';
echo $row['date'];
echo ')';
echo '<br>'; 
echo '<br>'; 


if((preg_match ("/\bjpg\b/",$row['media1'])) || (preg_match ("/\bjpeg\b/",$row['media1'])) || (preg_match ("/\bpng\b/i",$row['media1'])) || (preg_match ("/\bgif\b/i",$row['media1']))){

echo '<img style="max-width:300px;" src="media/news/'.$row['media1'].'" class="floatLeftClear" id="border">';

}

if((preg_match ("/\bjpg\b/",$row['media2'])) || (preg_match ("/\bjpeg\b/",$row['media2'])) || (preg_match ("/\bpng\b/i",$row['media2'])) || (preg_match ("/\bgif\b/i",$row['media2']))){

echo '<img style="max-width:300px;" src="media/news/'.$row['media2'].'" class="floatLeftClear" id="border">';

}



if((preg_match ("/\bmp3\b/", $row['media1']))) {

    echo ' <p id="audioplayer_'.$number1.'" class="floatLeftClear">Media Content</p>  
        <script type="text/javascript">  
        AudioPlayer.embed("audioplayer_'.$number1.'", {soundFile: "http://kiddiessupportscheme.org/media/news/'.$row['media1'].'"});  
        </script>';
        echo '<br>';

}
if((preg_match ("/\bmp3\b/", $row['media2']))) {

    echo ' <p id="audioplayer_'.$number2.'" class="floatLeftClear">Media Content</p>  
        <script type="text/javascript">  
        AudioPlayer.embed("audioplayer_'.$number2.'", {soundFile: "http://kiddiessupportscheme.org/media/news/'.$row['media2'].'"});  
        </script>';
        echo '<br>';

}

echo stripslashes(nl2br($row['text']));
echo '<br><br>'; 
echo stripslashes($row['author']);
echo '</div>';


echo '<p align="right" id="seperater"><a href="index2.php?op=News#top">Top</a><img src="images/seperater.jpg" width="950" height="6" style="border:none;"  /></p>';

}
$number1++;
$number1++;
$number1++;
$number2++;
$number2++;
$number2++;
$counter++;
}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别靠近我心 2024-11-12 15:37:39

对于你的第一个问题:

第一个问题是,如果新闻文本没有填满新闻图像旁边的所有空间,那么下一个新闻项目就会显示得太快(不在橙色断路器下方),正如您在网站上看到的那样片刻。我想解决这个问题,我可以将每个新闻帖子 div 的高度设置为图像的高度,尽管图像比 div 短一点,所以我不确定如何做到这一点。

你只需要编辑你的css文件:

更改:

#seperater {
  float: left;
}

到:

#seperater {
  clear: both;
}

对于你的第二个问题,我找到了这个链接: http://www.sitepoint.com/forums/3713338-post5.html 这正是您需要的解决方案。

我上面发布的链接中的代码:

define( 'LINK_LIMIT', 30 );
define( 'LINK_FORMAT', '<a href="%s" rel="ext">%s</a>' );

function prase_links  ( $m )
{
  $href = $name = html_entity_decode($m[0]);

  if ( strpos( $href, '://' ) === false ) {
    $href = 'http://' . $href;
  }

  if( strlen($name) > LINK_LIMIT ) {
    $k = ( LINK_LIMIT - 3 ) >> 1;
    $name = substr( $name, 0, $k ) . '...' . substr( $name, -$k );
  }

  return sprintf( LINK_FORMAT, htmlentities($href), htmlentities($name) );
}

$s = 'Here is a text - www.ellehauge.net - it has some links with e.g. comma,  www.one.com,
in it. Some links look like this: http://mail.google.com - mostly they end with a
space or carriage return www.unis.no
<br /> - but they may also end with a period: http://ellehauge.net. You may even put
the links in brackets (www.skred-svalbard.no) (http://one.com).
From time to time, links use a secure protocol like https://gmail.com |
This.one.is.a.trick. Sub-domaines: http://test.ellehauge.net |
www.test.ellehauge.net | Files: www.unis.no/photo.jpg |
Vars: www.unis.no?one=1&~two=2 | No.: www.unis2_check.no/doc_under_score.php |
www3.one.com | another tricky one:
http://ellehauge.net/cv_by_id.php?id%5B%5D=105&id%5B%5D=6&id%5B%5D=100';

$reg = '~((?:https?://|www\d*\.)\S+[-\w+&@#/%=\~|])~';
print preg_replace_callback( $reg, 'prase_links', $s );

to your first problem:

First problem is if the news text doesn't fill all the space by the side of the news image, then the next news item gets displayed too soon (not below the orange breaker) as you can see on the site at the moment. I was thinking to get around this I could set the height of each news post div to the height of the image, although the image is a little shorter than the div so I'm not sure how I'd do that.

You only have to edit your css file:

Change:

#seperater {
  float: left;
}

To:

#seperater {
  clear: both;
}

For your second problem, I found this link: http://www.sitepoint.com/forums/3713338-post5.html There is exactly the solution you need.

Code from the link I posted above:

define( 'LINK_LIMIT', 30 );
define( 'LINK_FORMAT', '<a href="%s" rel="ext">%s</a>' );

function prase_links  ( $m )
{
  $href = $name = html_entity_decode($m[0]);

  if ( strpos( $href, '://' ) === false ) {
    $href = 'http://' . $href;
  }

  if( strlen($name) > LINK_LIMIT ) {
    $k = ( LINK_LIMIT - 3 ) >> 1;
    $name = substr( $name, 0, $k ) . '...' . substr( $name, -$k );
  }

  return sprintf( LINK_FORMAT, htmlentities($href), htmlentities($name) );
}

$s = 'Here is a text - www.ellehauge.net - it has some links with e.g. comma,  www.one.com,
in it. Some links look like this: http://mail.google.com - mostly they end with a
space or carriage return www.unis.no
<br /> - but they may also end with a period: http://ellehauge.net. You may even put
the links in brackets (www.skred-svalbard.no) (http://one.com).
From time to time, links use a secure protocol like https://gmail.com |
This.one.is.a.trick. Sub-domaines: http://test.ellehauge.net |
www.test.ellehauge.net | Files: www.unis.no/photo.jpg |
Vars: www.unis.no?one=1&~two=2 | No.: www.unis2_check.no/doc_under_score.php |
www3.one.com | another tricky one:
http://ellehauge.net/cv_by_id.php?id%5B%5D=105&id%5B%5D=6&id%5B%5D=100';

$reg = '~((?:https?://|www\d*\.)\S+[-\w+&@#/%=\~|])~';
print preg_replace_callback( $reg, 'prase_links', $s );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文