0

I have the following code as shown below.
The code is working but the bottom part seems highly inefficient to me so I was wondering if anyone has any ideas how I can rewrite the code so that its not required to create a

baseUrl + '<?php echo $urls[0]; ?>',

for every row?

(the $urls contain only text like in this way: something-more (so no http: etc))

<?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>
6
  • 4
    There's a separate site for these types of questions, over at codereview.stackexchange.com. This should probably be moved. Commented Sep 8, 2014 at 10:00
  • 2
    use PDO instead mysql_* Commented Sep 8, 2014 at 10:00
  • what's not efficient about it ? If your array should have baseurl then it should have it. do you want something like factorization ? Commented Sep 8, 2014 at 10:01
  • So, you want to rewrite it to a for loop? Commented Sep 8, 2014 at 10:04
  • @Sekai: What is inefficient about it is that this code is unsafe (PHP to JS values are best json_encode-ed. fetching an array, and passingit on to JS is easily done by writing var someArr = <?= json_encode($urls); ?> job done Commented Sep 8, 2014 at 10:04

3 Answers 3

1

You could use json_encode after generating the array (including baseUrl) server-side :

<?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>
Sign up to request clarification or add additional context in comments.

Comments

0

You can do something like this in your 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); }

Now in your javascript:

var mySwipe = $('#pageBody').responsiveSwipe({
    edition: [
        baseUrl,
        <?php echo $java_urls;?>
    ],
    widthGuess: 0,
    emulator: window.location.hash.match(/emulator/)
});

1 Comment

You probably want to wrap $url in quotes.
0

This:

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]; ?>'
    ],

Could also be something like

var mySwipe = $('#pageBody').responsiveSwipe({
   edition: [
   <?php
   foreach($urls as $url) {
      echo '/'.$url.',';
   }
   ?>
   ]
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.