我有以下代码,如下所示 代码正在运行但底部对我来说效率非常低,所以我想知道是否有人有任何想法如何重写代码以便它不需要创建
baseUrl + '<?php echo $urls[0]; ?>',
每一行?
($ urls只包含这样的文字:更多东西(所以没有http:等等))
<?php
include("includes/connect.php");
$sql = mysql_query("SELECT url FROM urls ORDER BY listorder ASC");
$urls = array();
while ($rw = mysql_fetch_array($sql)) {
$urls[] = $rw['url'];
}
?>
<script>
var baseUrl = '/';
var mySwipe = $('#pageBody').responsiveSwipe({
edition: [
baseUrl,
baseUrl + '<?php echo $urls[0]; ?>',
baseUrl + '<?php echo $urls[1]; ?>',
baseUrl + '<?php echo $urls[2]; ?>',
baseUrl + '<?php echo $urls[3]; ?>',
baseUrl + '<?php echo $urls[4]; ?>',
baseUrl + '<?php echo $urls[5]; ?>',
baseUrl + '<?php echo $urls[6]; ?>',
baseUrl + '<?php echo $urls[7]; ?>',
baseUrl + '<?php echo $urls[8]; ?>',
baseUrl + '<?php echo $urls[9]; ?>',
baseUrl + '<?php echo $urls[10]; ?>'
],
widthGuess: 0,
emulator: window.location.hash.match(/emulator/)
});
$(document).ready(function(){
$(document).on('click', '.next', function (e) {
e.preventDefault();
mySwipe.gotoNext();
});
$(document).on('click', '.prev', function (e) {
e.preventDefault();
mySwipe.gotoPrev();
});
})
</script>
答案 0 :(得分:1)
您可以在生成数组(包括baseUrl)服务器端后使用json_encode
:
<?php
...
$baseUrl = '/';
$urls = array($baseUrl);
while ($rw = mysql_fetch_array($sql)) {
$urls[] = $baseUrl.$rw['url'];
}
?>
<script>
var mySwipe = $('#pageBody').responsiveSwipe({
edition: <?php echo json_encode($urls); ?>,
widthGuess: 0,
emulator: window.location.hash.match(/emulator/)
});
...
</script>
答案 1 :(得分:0)
你可以在你的php中做这样的事情:
$java_urls = '';
foreach ($urls as $url)
{
$java_urls .= "'/".$url."',";
}
// remove last , from the string
if ($java_urls!='') { $java_urls = substr($java_urls, 0, -1); }
现在在你的javascript:
var mySwipe = $('#pageBody').responsiveSwipe({
edition: [
baseUrl,
<?php echo $java_urls;?>
],
widthGuess: 0,
emulator: window.location.hash.match(/emulator/)
});
答案 2 :(得分:0)
此:
var mySwipe = $('#pageBody').responsiveSwipe({
edition: [
baseUrl,
baseUrl + '<?php echo $urls[0]; ?>',
baseUrl + '<?php echo $urls[1]; ?>',
baseUrl + '<?php echo $urls[2]; ?>',
baseUrl + '<?php echo $urls[3]; ?>',
baseUrl + '<?php echo $urls[4]; ?>',
baseUrl + '<?php echo $urls[5]; ?>',
baseUrl + '<?php echo $urls[6]; ?>',
baseUrl + '<?php echo $urls[7]; ?>',
baseUrl + '<?php echo $urls[8]; ?>',
baseUrl + '<?php echo $urls[9]; ?>',
baseUrl + '<?php echo $urls[10]; ?>'
],
也可能像
var mySwipe = $('#pageBody').responsiveSwipe({
edition: [
<?php
foreach($urls as $url) {
echo '/'.$url.',';
}
?>
]
})