0

the code below works fine it updates the rank in db when submit button is clicked, but then it refreshes the page and loads the old content itself instead of updating with new content. how do i get it to load the new content on refresh.

     <?php

        function update_ranks($newimgurl,$newrank){

        switch ($newrank)
        {
        case "1":
              $rank="1st";
              break;
        case "2":
              $rank="2nd";
              break;
        case "3":
              $rank="3rd";
              break;

        }


                $field_key = "field_582fdg46";  
                $value=array();
                $count=0;
                global $totalimgs;
                global $allimgurl;          
                        global $allimgranks;               

                while ($count < $totalimgs){

                if ($allimgurl[$count]==$newimgurl){
                $value[] = array("imglink" => $newimgurl,"rank" => $rank );

                }
                else{
                $value[] = array("imglink" => $allimgurl[$count],"rank" => $allimgranks[$count] );      
                }
                $count=$count+1;        
                update_field( $field_key, $value, $post->ID);       

                }

                unset($newrank,$newimgurl,$currentnewrank,$currentimgurl,$rank,$count); 
                unset($allimgurl,

    ,$allimgranks,$totalimgs,$value);   


    }
?>
<?php
        if(isset($_POST['rank1btn'])){
        $currentnewrank=$_POST['rank1'];
        $currentimgurl=$rank1img;
        update_ranks($currentimgurl,$currentnewrank);
        }
        if(isset($_POST['rank2btn'])){
        $currentnewrank=$_POST['rank2'];
        $currentimgurl=$rank2img;
        update_ranks($currentimgurl,$currentnewrank);
        }
        if(isset($_POST['rank3btn'])){
        $currentnewrank=$_POST['rank3'];
        $currentimgurl=$rank3img;
        update_ranks($currentimgurl,$currentnewrank);
        }


    ?>   

     <form id="myform" name="myform" action="" method="POST" >
        <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12 ">

        <?php   
        global $post;

        $bookid=get_the_title($post->ID);;
        $args = get_posts(array('post_type' => 'bookgallery' ,'post_title' =>$bookid, 'posts_per_page' => -1
        ));
        ?>

        <?php
        foreach ( $args as $post ) :  setup_postdata($post);

        if (!empty($post))
            {  
            $allimg=$allrank=array();

            while( have_rows('imgs') ): the_row();
            $temprank=get_sub_field('rank',$post->ID);  
            $tempimg=get_sub_field('imglink',$post->ID);

            $allimg[]=$tempimg;
            $allrank[]=$temprank;

            if ($temprank=='1st') {
            $rank1img= $tempimg;
            $rank1='1st';
            } 
            if ($temprank=='2nd') {
            $rank2img= $tempimg;
            $rank2='2nd';
            } 
            if ($temprank=='3rd') {
            $rank3img=$tempimg;
            $rank3='3rd'
            } 

            endwhile;

            if (!empty($rank1img)){
            ?>
            <div >

            <img src="<?php echo $rank1img; ?>" alt="" >

            <div >1st
                <select name="rank1">
                    <option value="0"> </option>
                    <option value="1">1st</option>
                    <option value="2">2nd</option>
                    <option value="3">3rd</option>    
                        </select>
            </div>   

            <div ><input type="submit" name="rank1btn" value="update rank" id="rank1btn" ></div>   
            </div>   
            <?php } 
            if (!empty($rank2img)){
            ?>
            <div >2nd

            <img src="<?php echo $rank2img; ?>" alt="" > 

            <div >
                <select name="rank2">
                    <option value="0"> </option>
                    <option value="1">1st</option>
                    <option value="2">2nd</option>
                    <option value="3">3rd</option>
                        </select>
            </div>   

            <div ><input type="submit" name="rank2btn" value="update rank" id="rank2btn" ></div>     
            </div>
             <?php } 
            if (!empty($rank3img)){
            ?>   

             <div>3rd  

            <img src="<?php echo $rank3img; ?>" alt="" >   
            <div>
                <select name="rank3">
                    <option value="0"> </option>
                    <option value="1">1st</option>
                    <option value="2">2nd</option>
                    <option value="3">3rd</option>
                        </select>
            </div>   

            <div><input type="submit" name="rank3btn" value="update rank" id="rank3btn" ></div>     
            </div>
             <?php } 

            }
         endforeach; ?>

        </article> 
        </form>

i put the update_ranks first below that the isset code and below tht the form...now it doesn't even update the database whereas when i had the isset code below the form it used to update the db but the page refresh tht happens automatically didnt show the new content.u had to manually refresh the page to show the new content. how do i get it to load the new content automatically along with updating the db.

  • Clear your posts array at the bottom of your page? `` – Sam Orozco Apr 18 '16 at 06:42
  • Possible duplicate of question [http://stackoverflow.com/questions/36685121/unable-to-run-php-function-to-update-db-value-using-document-write](http://stackoverflow.com/questions/36685121/unable-to-run-php-function-to-update-db-value-using-document-write) – VipindasKS Apr 18 '16 at 06:42
  • You show the posts and after that you process the POST request if it is set. So process a POST request first and then get the current data and show it. – jeroen Apr 18 '16 at 06:43
  • VinpindasKS...this is a different approach using only php – Manasi Nigam Apr 18 '16 at 07:02

2 Answers2

2

You are updating the database on the bottom of the page, so the content is loaded before your update query has been executed. This can be resolved by placing the update query above your html content.
This way the update will execute and after that you will load the new data from the database.

Remember that all code will always execute in the order it is placed in the file, even the rendering of html content.

Jerodev
  • 32,252
  • 11
  • 87
  • 108
0

I made a total newbie mistake. I have two copies of my files. One which my editor changes. The other I need to copy under nginx web directory /var/www/html/. I failed to copy my changes files, so nothing was updated.

I know this is a different root cause than the original question, but I'm afraid, probably a common reason why some people will not see their changes reflected (and hence, end up on stackoverflow).

thebiggestlebowski
  • 2,610
  • 1
  • 33
  • 30