1

I am new to javascript and I'm trying to pass image from database into javascript, but I can not.

The problem code is :

'<img src="<?php echo base_url().'./images/burger/'.$val->image ?>">';

and this is my code

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<div id="box">
    <img id="image" />
</div>
<?php
$query = $this->db->get('product');
foreach($query->result() as $val):
?>

<script>

    var images =
    '<img src="<?php echo base_url().'./images/burger/'.$val->image ?>">';

function randImg() {
    var size = images.length
    var x = Math.floor(size * Math.random())
    document.getElementById('image').src = images[x];
}

randImg();
</script>
<?php endforeach; ?>
</body>
</html>
7
  • If I am not mistaken, you are using codeigniter, need to know what is error saying... Commented Nov 6, 2015 at 8:43
  • 1
    for codeigniter it ok. but pass image into javascript I can not Commented Nov 6, 2015 at 8:44
  • 2
    @MarmikBhatt you are not mistaken. The 'codeigniter' tag gives it away.. Commented Nov 6, 2015 at 8:44
  • 1
    Event delete quote it still not work Commented Nov 6, 2015 at 8:46
  • Yeah I saw you were try to do javascript, that part is okay I think. Commented Nov 6, 2015 at 8:47

2 Answers 2

2
<!DOCTYPE html>
<html>
    <head>
        <title>Javascript random image</title>
        <?php
            /* Query the db */
            $query = $this->db->get('product');
            /* use php to generate the javascript array of images from db query */
            echo "
            <script type='text/javascript'>
                var images=[];
                var baseurl='".base_url()."';
                var path='./images/burger/';";

            foreach( $query->result() as $val ){
                /* Add image to javascript array */
                echo "images.push('{$val->image}');\n";
            }

            echo "
            </script>";
        ?>  
        <script type='text/javascript'>

            function rnd_index(a,b) {
                return Math.round( a + ( Math.random() * ( b - a ) ) );
            }
            function randImg() {
                var x = rnd_index( 0, images.length-1 );
                document.getElementById('image').src = baseurl + path + images[ x ];
            }

            function orig__randImg() {
                var size = images.length;/* This might be too large sometimes for the array */
                var x = Math.floor( size * Math.random() );
                document.getElementById('image').src = path + images[ x ];
            }
            /* Load a random image when the page has loaded */
            window.onload=randImg;
        </script>
    </head>
    <body>
        <div id="box">
            <img id="image" />
        </div>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

My only concern there would be that the random number that selects from the array MIGHT be, on occasion, too high and cause an error. Take a look at the edited / amended version.
in the var path='./images/burger/';";
beacase when I link to other page can not see the picture
2

You need to create an array in javascript and push the image path.

Before <?php foreach(...

<script>
    var images = [];
    function randImg(images) {
        var size = images.length
        var x = Math.floor(size * Math.random())
        document.getElementById('image').src = images[x];
    }
</script>

Inside the loop..

<script>
    images.push("<?php echo base_url().'./images/burger/'.$val->image ?>");
</script>

After loop...

<script>
    randImg(images);
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.