从javascript生成URL的问题

时间:2013-03-24 09:04:07

标签: php javascript jquery html mysql

我试图通过从数据库中获取值来生成多个链接。 我可以连接到数据库没有任何问题,我可以毫无问题地提取变量,但是当我尝试生成我的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。这个额外的&lt; li&gt;&lt; 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>

1 个答案:

答案 0 :(得分: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仍然存在问题,你不能像这样混合它来实现我认为你想要做的事情...... 但试试看看它是否有效: )