-1

I made a PHP file that queries data from mysql database and puts results into html table. One field on database contains links to file(s). One or more, separated with commas. Everything works great. (Using: GROUP_CONCAT(CONCAT(filename)) in SQL query. Now the tricky part. I tried to use Bootstrap Modals. My main goal is - when i click the button i'll get small popup with links inside. But i only get one link from the first result in a table. When i sort the tabel somehow - same thing happens. Here's the little output from table that i have right now:

8   Company 1     Address 12-11/12, City    2017-04-16  12/022  Link    
9   Company 2     P. Address 19-M1,City 2020-03-31  15/039  Link    
10  Company 3     Address 1,City    2017-04-05  12/015  Link,Link,Link,Link

So, the goal is: button, when i click on it popup will appear with links inside it. Any hints how to achieve that? Maybe additional query by id or something like that?

von Oak
  • 823
  • 5
  • 14
Jaur
  • 85
  • 1
  • 13
  • I would create a php function to retrive the links of the id of the listing (or what ever it should be), and call this function with the id (of item of which button is clicked) via an ajax call when the model is shown. – Smort Jul 31 '16 at 19:48
  • This may be interesting for you: http://stackoverflow.com/questions/18378720/bootstrap-3-with-remote-modal – Smort Jul 31 '16 at 19:55

2 Answers2

1

worked test it

<?php
$arr = [
    ["Company 1", "Address 1,City", "2017-04-05", "12/015", "Link"],
    ["Company 3", "Address 1,City", "2017-04-05", "12/015", "Link,Link,Link,Link"]
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Example of Bootstrap 3 Modals</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function(){
    $(".btn").click(function(){
        $("#myModal").modal('show');
    });
});
</script>
<style type="text/css">
    .bs-example{
        margin: 20px;
    }
</style>
</head>
<body>
<div class="bs-example">
    <!-- Button HTML (to Trigger Modal) -->
    <a href="#" class="btn btn-lg btn-primary">Launch Demo Modal</a>

    <!-- Modal HTML -->
    <div id="myModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">

                <table>
                  <tr>
                    <th>name</th>
                    <th>address</th>
                    <th>date</th>
                    <th>data</th>
                    <th>links</th>
                  </tr>
                  <tr>
                      <?php foreach ($arr as $value): ?>
                        <tr>
                          <td><?= $value[0]; ?></td>
                          <td><?= $value[1]; ?></td>
                          <td><?= $value[2]; ?></td>
                          <td><?= $value[3]; ?></td>
                        <td>
                        <?php if (strpos($value[4], ',') == false AND ! empty($value[4])): ?>
                          <a href="<?= $value[4]; ?>">link name</a>
                        <?php else: ?>
                          <?php $links = explode(',', $value[4]); ?>
                          <?php foreach ($links as $link): ?>
                            <a href="<?= $link; ?>">link name</a>
                          <?php endforeach; ?>
                        <?php endif; ?>
                      </td>
                        </tr>
                      <?php endforeach; ?>
                </table>
                <table>

                </table>
            </div>
        </div>
    </div>
</div>
</body>
</html>                                     
knizer
  • 133
  • 1
  • 9
-1

Perhaps something like this?

if(isset($_POST['button'])) {
     $result = mysqli_query($conn,"SELECT * FROM table WHERE address='.$address' ");
     echo "<table>";
     while($row = mysqli_fetch_array($result))
     {
     echo "<tr>";
     echo '<td> <a href="' . $row['link1'] . '">Link1</a> </td>';
     echo '<td> <a href="' . $row['link2'] . '">Link2</a> </td>';
     echo '<td> <a href="' . $row['link3'] . '">Link3</a> </td>';
     echo "</tr>";
     }
     echo "</table>";
}

If you don't know how many links you'll need to echo, you would need a 2nd loop

BotHam
  • 17
  • 7
  • `.$address` looks like a typo. `$address` is undefined and looks to open the OP to SQL injections. – chris85 Jul 31 '16 at 20:10