0

我正在尝试通过从数据库中获取值来生成多个链接。我可以毫无问题地连接到数据库,并且可以毫无问题地提取变量,但是当我尝试生成我的 html 代码时,一切都变成了乱码。

<script type="text/javascript">
//Generate carousel
var mymovies = "<div id='tumbnailtest' class = 'carousel'><ul><li><a href='watchmovie.php?path=";
for (var i = 1; i < 10; i++){
    <?php
    //Get movie List
    $result = mysql_query("SELECT Title, Filepath, Posterpath FROM movies WHERE ID = $count");
    while ( $row = mysql_fetch_array($result) ) {
        //for generating url
        $mpath = $row["Filepath"];
        $mmname = $row["Title"];
        $ppath = $row["Posterpath"];
        $count = $count + 1;
    }
    ?>
    mymovies += "<li><a href=watchmovie.php?path=<?php echo $mpath;?>"
    mymovies += "&mname=<?php echo $mmname;?><img src=<?php echo $ppath;?> width='200' height='300' border='0' style='padding: 0 0px 0 0px;' title='<?php echo $mmname; ?>'></a></li>";
    }
    mymovies += "</ul></div>";
document.write(mymovies);

</script>

链接最终看起来像这样

<a href="watchmovie.php?path=&lt;li&gt;&lt;a href=watchmovie.php?path=Movies/iRobot.mp4&amp;mname=iRobot&lt;img src=images/iRobot.jpeg width=" 200'="" height="300" border="0" style="padding: 0 0px 0 0px;" title="iRobot"></a>

当它最终应该看起来像这样

<li><a href='watchmovie.php?path=<?php mname ?>&mname=<?php echo $mname;?>' target="_blank"><img id='' src='<?php echo $ppath; ?>' width="200" height="300" border="0" style="padding: 0 0px 0 0px;" title="<?php echo $mname; ?>"></a></li>

当 $count = 1 时,mname 是 iRobot。这个额外的 <li><a href=watchmovie.php?path= 来自哪里?似乎我将随机垃圾写入我的链接。

此处更新文件。

<?php
//connect to database server
$row;
    $dbcnx1 = mysql_connect("localhost", "root", "wouldntyouliketoknow");
    if (!$dbcnx1){
        echo( "<P>Unable to connect to the database server at this time.</P>" );
        exit();
    }
    else{
        echo( "<P>Successfully connected to the database!</P>" );
    }
    //select database
    mysql_select_db("movies", $dbcnx1);
    if (! @mysql_select_db("movies") ) {
        echo( "<P>Unable to locate the movies database at this time.</P>" );
        exit();
    } else {
        echo( "<P>Successfully connected to the movies database!</P>" );
    }
    //Get movie List
    $count = 1;
?>
<html>
<link rel="stylesheet" type="text/css" href="css/JMyCarousel.css" />
<script type='text/javascript' src="js/jquery.js"></script>
<script type='text/javascript' src='js/scollingcarouselunminified1.js'></script>
<script type="text/javascript">
$(document).ready(function() {
    $('#tumbnailtest').scrollingCarousel({
    autoScroll: false
    });
});
</script>
<?php
$mymovies = "<div id='tumbnailtest' class = 'carousel'><ul>";
for ($i = 1; $i <= 10; i++) {
//Get movie List
$result = mysql_query("SELECT Title, Filepath, Posterpath FROM movies WHERE ID = ".$i);
while ( $row = mysql_fetch_array($result) ) {
    //for generating url
    $mpath = $row["Filepath"];
    $mmname = $row["Title"];
    $ppath = $row["Posterpath"];
}
$mymovies .= "<li><a href='watchmovie.php?path=".$mpath;
$mymovies .= "&mname=".$mmname."'><img src='".$ppath."' width='200' height='300' border='0' style='padding: 0 0px 0 0px;' title='".$mmname."'></a></li>";
}
$mymovies .= "</ul></div>";
echo $mymovies;
?>

</html>
4

1 回答 1

0

将JS代码更改为:

<html>
<head>
    <link rel="stylesheet" type="text/css" href="css/JMyCarousel.css" />
</head>
<body>
<?php
//connect to database server
$row;
    $dbcnx1 = mysql_connect("localhost", "root", "wouldntyouliketoknow");
    if (!$dbcnx1){
        echo( "<P>Unable to connect to the database server at this time.</P>" );
        exit();
    }
    else{
        echo( "<P>Successfully connected to the database!</P>" );
    }
    //select database
    mysql_select_db("movies", $dbcnx1);
    if (! @mysql_select_db("movies") ) {
        echo( "<P>Unable to locate the movies database at this time.</P>" );
        exit();
    } else {
        echo( "<P>Successfully connected to the movies database!</P>" );
    }
?>
<script type='text/javascript' src="js/jquery.js"></script>
<script type='text/javascript' src='js/scollingcarouselunminified1.js'></script>
<script type="text/javascript">
$(document).ready(function() {
    $('#tumbnailtest').scrollingCarousel({
    autoScroll: false
    });
});
</script>
<?php
$mpath;
$mmname;
$ppath;
$mymovies = "<div id='tumbnailtest' class = 'carousel'><ul>";
for ($i = 1; $i <= 10; $i++) {
//Get movie List
$result = mysql_query("SELECT Title, Filepath, Posterpath FROM movies WHERE ID = '".$i."'");
while ( $row = mysql_fetch_array($result) ) {
    //for generating url
    $mpath = $row["Filepath"];
    $mmname = $row["Title"];
    $ppath = $row["Posterpath"];
}
$mymovies .= "<li><a href='watchmovie.php?path=".$mpath;
$mymovies .= "&mname=".$mmname."'><img src='".$ppath."' width='200' height='300' border='0' style='padding: 0 0px 0 0px;' title='".$mmname."'></a></li>";
}
$mymovies .= "</ul></div>";
echo $mymovies;
?>
</body>
</html>

JS和PHP仍然存在问题,你不能像这样混合它来实现我认为你正在尝试做的事情......但是试试这个,看看它是否有效:)

于 2013-03-24T09:12:52.503 回答