1

I'm a newbie developer. There is this code:

<?php

$sql_client = "SELECT * FROM clienti WHERE nume = ? LIMIT 1";
$stmt_client = $conn->prepare($sql_client);
$stmt_client->bind_param("s", $nume);
$stmt_client->execute();
$result_client = $stmt_client->get_result();
while($row = $result_client->fetch_assoc())
    {
?>
        <td style="width:35%;">
            <b>Cumpărător</b>:<br>
            <?php echo $row["nume"]; ?><br>
            <b>Nr Orc</b>: <?php echo $row["reg_com"]; ?><br>
            <b>CIF</b>: <?php echo $row["cif"]; ?><br>
            <b>Sediu</b>:<br>
            <?php echo $row["adresa"]; ?><br>
            <b>Banca</b>:<br>
            <?php echo $row["banca"]; ?><br>
            <b>Cont bancar</b>:<br>
            <?php echo $row["cont_bancar"]; ?><br>
        </td>
    </tr>
</table>

<?php
    }
?>

Code from second file

<?php 
$sql_client = "SELECT * FROM clienti WHERE nume = ? LIMIT 1";
$stmt_client = $conn->prepare($sql_client);
$stmt_client->bind_param("s", $nume);
$stmt_client->execute();
$result_client = $stmt_client->get_result();
while($row = $result_client->fetch_assoc())
    {
?>
            Am încasat de la <?php echo $row["nume"]; ?> <br>
            Nr ORC/an: <?php echo $row["reg_com"]; ?> <br>
            CIF: <?php echo $row["cif"]; ?><br>
            Adresa: <?php echo $row["adresa"]; ?> <br>
<?php
    }
?> 

As you can see, there's php code that is "interrupted" by html, then it continues by closing the curly brace of the while loop.

The problem is that I need to repeat the php code but not the html. The html code from within will be different the next time php runs (inside the html there are echo functions that retrieve different data from the loop results).

I tried putting the first chunk of code inside a function and run the function but it only screws up the layout of the page and doesn't show the part that the code should render.

My question is: how can I reuse that first chunk of incomplete code? Thank you!

5
  • Do you want to run a different query and loop through the results, or loop through the same results a second time? If you need to loop through the results again, you could use mysqli_data_seek() to set the pointer back to the start. Or you could specify <div>s in your HTML and populate them in the same loop, maybe. Commented Jun 5, 2020 at 18:05
  • The loop is the same. The html code is different. I could use include but that would need extra files that complicate the file structure. Commented Jun 5, 2020 at 18:23
  • Add conditions inside the loop so you can load what you need to. If you need to use data from previous loops, put the information inside variables so you can use it. Commented Jun 5, 2020 at 18:30
  • Onimusha, I think that would be the solution. But I can't figure out how to dump the values from while into variables. Did some googling around but with no success. Commented Jun 5, 2020 at 19:06
  • Are you using include() to include both of these files into a thrid file? Is there a reason you have to do it that way, rather than just writing what you need in that third file? Commented Jun 5, 2020 at 20:47

1 Answer 1

2

Instead of starting and stopping your PHP code, have you looked at simply echoing the HTML code that you want to include? It might help you organize what you want repeated in the loop and what you do not. Here's an example:

$myArray = [];   //array that will hold the values you get from database for later use

$sql_client = "SELECT * FROM clienti WHERE nume = ? LIMIT 1";
$stmt_client = $conn->prepare($sql_client); 
$stmt_client->bind_param("s", $nume);
$stmt_client->execute();
$result_client = $stmt_client->get_result();

echo '<table>';                            //does not repeat
while($row = $result_client->fetch_row())
    {   
        array_push($myArray, $row);        //add each row to an array outside the scope of your loop
        echo '<tr>';                       //repeats once for each table row
        foreach($row as $columnValue){
            echo '<td><p>'.$columnValue.'</p></td>';  //repeats for every value in table
        }
        echo '</tr>';
    }
echo '</table>';                            //does not repeat

echo $myArray[0][0];   //echo first value of first row 
Sign up to request clarification or add additional context in comments.

10 Comments

I did that but that is not my problem. I need to repeat the partial php code. Some of the html is in other files. I don't want to fill all those files with the same code. I'd rather repeat. The structure is this: - there is a main file that only includes code from 2 other files - one of those files has the loop + 1 html chunk - the other would look simple if it just repeated the partial code using something like a variable, function or similar (one line command - that's the idea), have some html that contains the echos, than close the php with closing the curly brace.
You're going to need to show all of that code in your question. It's far too abstract to explain it in three sentences here and expect someone to figure it out for you.
All of the code is 3 files long. I need to reuse results from a while query. The problem is that after the loop closes, later on I cannot access the results anymore. I think this is the simplest way of saying it.
Well, at least show the relevant parts from each file so that we can see how they relate to each other. Is the code that you've shown being re-used in a loop within another file?
I'll give a diverting answer (sorry about that): the code shown at the beginning takes data from MySQL. In the html, I nested <? php echo $row["blabla"]; ?> Then the loop closes. In the other file, I need to reuse some of those values. Tried repeating the echo stuff but with no results. PS: I don't see how I can shove all the code here. After writing this, I have 200 characters left. I feel stupid.
|

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.