1

I have faced small problem regarding passing php variable outside of PHP loop. I have created table and form in same page. First table is displayed and the through edit link in form pop up dialog box appears. I want to pass dynamic ID to dialog box after click edit link. My page includes a php table and form.

PHP/HTML Table

<table>
    <th>ID</th>
    <th>Title</th>
    <th>Edit</th>
        <?php
            $query="select * from video order by id desc";
            $result=$con->query("SELECT * FROM video ORDER by id DESC") or die($con->error);
            while($row=$result->fetch_assoc()){
                $id=$row['id'];
                $heading=$row['heading'];
        ?>
    <tr>
        <td>
            <?php echo $id ?>
        </td>
        <td>
            <?php echo $heading ?>
        </td>
        <td>
            <a href="#modal2" id="pop_button">Edit</a>
        </td>
        </tr>
        <?php } ?>
</table>

After this table, I want to display data in this form

PHP Form in dialog box

<div class="remodal" data-remodal-id="modal2" role="dialog" aria-labelledby="modal2Title" aria-describedby="modal2Desc">
    <form action="" method="POST" enctype="multipart/form-data">
        <table>
        <tr>
            <th>Video ID</th>
            <td>
                <input type="number" value="<?php echo $id?>" name="id" readonly></input>
            </td>
        </tr>
        <tr>
            <th>Video Title</th>
            <td>
                <input type="text" value="<?php echo $heading?>" name="title"></input>
            </td>
        </tr>
        </table>    
    </form> 
</div>

I know that while loop can't connect to popup dialog box form, so my code prints only data of first row. If I include whole code within while loop, my table structure become mess. Is there any idea to pass ID outside of while loop ? I tried using global variable method, but it doesn't work. Perhaps I can't properly use global variable. Thanks

8
  • 1
    Have you tried a second loop to make all the popups? Commented Aug 29, 2017 at 12:07
  • are you creating as many modals equal to no of records u getting from select? Commented Aug 29, 2017 at 12:10
  • After the loop the variables you assigned values to inside the loop, of course only have the last value they have been assigned. But you could for example put this data in an array, so that you can loop over that again after your while loop. But you would also have to modify the ids you are using for your HTML elements, because ids have to be unique within an HTML document. Commented Aug 29, 2017 at 12:10
  • 1
    @FMashiro is right, try a second loop. You can create an array with the first loop array[$id] = $heading and then iterate through that later. Commented Aug 29, 2017 at 12:21
  • @FMashiro second loop for popups doesn't work for me because first loop ID is main output. On the basis of ID, it should display mysql data. Commented Aug 29, 2017 at 15:59

1 Answer 1

1

In your while loop store values in to array like this:

<?php
            $query="select * from video order by id desc";
            $result=$con->query("SELECT * FROM video ORDER by id DESC") or die($con->error);
            $array = [];            
            while($row=$result->fetch_assoc()){
                $id=$row['id'];
                $heading=$row['heading'];
                $array[$id] = $heading;

        ?>

And in your html table like this :

    <table>
            <tr>
                <th>Video ID</th>
                <th>Video Title</th>
                <?php foreach ($array as $key => $val){ ?>
                <td>
                    <input type="number" value="<?php echo $key?>" name="id" readonly></input>
                </td>
                <td>            
                    <input type="text" value="<?php echo $val?>" name="title"></input>
                </td>           
                <?php } ?>
            </tr>
  </table> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help @Nawin. But your code displays all row at once. Infact, I want to show only specific row which is clicked. My first code displays only recent mysql row, and I tried by using your code, it displays all rows that are in mysql table. How to display only specific row which is clicked. If id 5 is clicked, it should echo only data of row 5. How can I make it possible ?

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.