1

I'm trying to fetch the previous row of data from the database select statement in the current data set.

My current code looks like this:

<?php
            $lib = $_GET["lib"];

            include "connect.php";

            $sql = "SELECT Image, text, link FROM library WHERE Type = '$lib'";
            $result = $conn->query($sql);

            $count = 1;
            if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                        if ($count <= 6) {
                            if ($count == 1) {
                                echo "<tr>";
                            }
                                    echo "<td align='center'>";
                                        echo "<a href='".$row["link"]."'><img src='".$row["Image"]."'/></a>";
                                    echo "</td>";
                                $count++;
                            if ($count == 7) {
                                echo "</tr>";
                            }
                        } else if ($count >= 7 && $count <= 12) {
                            if ($count == 7) {
                                echo "<tr>";
                            }   
                                    echo "<td align='center'>";
                                        echo"<a href='".$row["link"]."'><p>".$row["text"]."</p></a>";
                                    echo "</td>";
                                $count++;
                                if($count == 13){
                                    $count = 1;
                                }
                            if ($count == 1) {
                                echo "</tr>";
                            }

                        }                   
                }
                echo "</tr>";
            } else {
                echo "No Videos in the ".$lib." Section at this time!";
            }
            $conn->close();
        ?>

So the first 6 rows will output, then the 7 - 12th rows will output but will use the selected data from the previous 6 row results.

for example .$row["variable"][-6 results]. I know these a method of doing this but i cannot remember the way to handle the array that is returned from the database when the select statement is performed.

I was thinking about using $result->data_seek(0); but am not sure how to use it in this case.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Chris.H
  • 45
  • 1
  • 6
  • 1
    Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Jun 16 '17 at 20:52
  • Thank you for the advise, will be looking into beefing up the code after I have the concept working. its been a while so "dipping my feet" back into coding after a 3 year break. – Chris.H Jun 16 '17 at 20:58
  • 1
    That the obvious answer to the question asked in the title is, well if you need something later on, then store it into a variable ... is rather obvious, isn't it? – CBroe Jun 16 '17 at 21:01

1 Answers1

0

I've worked out that I'm over complicating things. Turns out that the following code does what I needed. just needed to include the text in the same under the image, instead of having a new row for just the text.

            <?php
            $lib = $_GET["lib"];

            include "connect.php";

            $sql = "SELECT Image, text, link FROM library WHERE Type = '$lib'";
            $result = $conn->query($sql);

            $count = 1;
            if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                            if ($count == 1) {
                                echo "<tr>";
                            }
                                echo "<td align='center'>";
                                    echo "<a href='".$row["link"]."'><img src='".$row["Image"]."'/></a>";
                                    echo"<a href='".$row["link"]."'><p>".$row["text"]."</p></a>";
                                echo "</td>";
                            if ($count == 6) {
                                echo "</tr>";
                                $count = 0;
                            }
                            $count++;
                            echo $count;

                }
                echo "</tr>";
            } else {
                echo "No Videos in the ".$lib." Section at this time!";
            }
            $conn->close();
        ?>

Thanks to anyone who was looking into this for me.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Chris.H
  • 45
  • 1
  • 6