Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a website with some images that rotates when a button (img) is clicked on. These images then change src. This src is put into an array but for the buttons to know what image is clicked on, I will need the array index I suppose. I am actually not sure that this is the best approach, but it's the only way I could think of. I have some AJAX here:

<script type="text/JavaScript">

function rotateObject(e)
{
    //e is handler which contains info about the item clicked. From that we can obtain the image id.
    //since the id are of the form img_123(some number), we need to extract only the number.
    var img_id = e.id.split("_")[1];
    var img_src = "<?php echo $arr['number'][0]['src'];?>"; //Tried this 
    var img_rotate = <?php echo (($arr['rotation'] + 1) % 4); ?>;

    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var getEle = document.getElementsByClassName('item' + img_id)[0];
            var imagePath = img_src + img_rotate + ".png";
            getEle.src = imagePath + xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST","database/update_settings_rotate.php",true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("item_id="+encodeURIComponent(img_id));
}
</script>

As you can see, I am trying to save a variable named img_src. This was originally just equal to $arr['src'] but I have been trying to tell the AJAX which src I am talking about in the array and therefore tried adding a $arr['number'] which didn't work.. I guess you want to see my arrays too, they are here:

$item_number = 0;
                $rowsize = 12;

                $itemArray = array();
                $finalArray = array();
                $results = 0;

                    for ($i = 0; $i < $rowsize; $i++) {

                        $stmt = $mysqli->stmt_init();
                        $stmt->prepare('SELECT z, rotation, src, name FROM house_room1 INNER JOIN objects ON house_room1.object_id=objects.object_id WHERE house_room1.ref_id = ?'); 
                        $stmt->bind_param('i', $i
                        );
                        if ($stmt->execute()) {
                            $stmt->bind_result($z, $rotation, $src, $name);
                                while($stmt->fetch()) {

                                   $results = 1;
                                   $itemArray['number'] = $item_number;
                                   $itemArray['name'] = $name;
                                   $itemArray['ref_id'] = $z;
                                   $itemArray['rotation'] = $rotation;
                                   $itemArray['src'] = $src;
                                   array_push($finalArray, $itemArray);

                                }
                                }
                                else {
                                    echo 'Something went terribly wrong' . $mysqli->error;
                                        }
                                $stmt->close();

                                $item_number++;
                            }

                            if($results == 1){
                            aasort($finalArray,"ref_id");

                                foreach($finalArray as $arr){
                                    echo '<li id="item-' . $arr['number'] . '" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' . $arr['name'] . ' 
                                    <img class="rotate" id="img_'.$arr['number'].'" src="images/house/other/settings.jpg" onclick="rotateObject(this)">';
                                }
                            }

                            //create a function for sorting
                            function aasort (&$array, $key) {
                                $sorter=array();
                                $ret=array();
                                reset($array);
                                foreach ($array as $ii => $va) {
                                    $sorter[$ii]=$va[$key];
                                }
                                asort($sorter);
                                foreach ($sorter as $ii => $va) {
                                    $ret[$ii]=$array[$ii];
                                }
                                $array=$ret;
                            }

If I don't specify the specific array index, the arr['src'] will just be equal to the last src mentioned in the array. I hope I can find an answer on my problem here, thanks in advance. Btw. I am open for other solutions to this problem as well, everything is appreciated. Thanks!

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.