0

I have a little issue when I try to to loop my php values in HTML. So far this is what I tried but I have not excpected result.

If I remove the loop I only get the first entry. I would like to echo all the possibles entries from my research. This is my code ( from an SQL request).

<html>
               <table>
                        <tr>
                            <th>Name</th>
                            <th>Surname</th>
                            <th>Number</th>
                            <th>Adress</th>
                            <th>link</th>

          <!--<th class="glyphicon glyphicon-pencil"></th>-->
                   </tr>
                        <tr>
                <?php $rowG = oci_fetch_array($stid, OCI_RETURN_NULLS);?>
                <?php foreach($array as $rowG=>$value): ?> <tr>
                            <td><?php echo $rowG[2]; ?></td>
                            <td><?php echo $rowG[1]; ?></td>
                            <td><?php echo $rowG[0]; ?></td>
                            <td><?php echo $rowG[3]?></td>
                            <td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>

            <?php endforeach;}} ?>

                       </tr>
                    </table>

</html>

Do you know where I made my mistake ?

Thank you for your help

Edit : Finally I managed to do it by using a do{}while loop.

Thank you all for your help

RFlow

2
  • $array? What is this? Commented Aug 4, 2017 at 14:46
  • 3
    foreach($rowG as $array=>$value). First it goes the array name and then the current element name. Change those as well: <?php echo $rowG[0,1,2,3...]; ?> to <?php echo $array[0,1,2,3...]; ?>. Take a look at @u_mulder's answer. Much better solution. Commented Aug 4, 2017 at 14:47

3 Answers 3

1

It's hard to guess what you are trying to do or even what actually happens, since I don't know what is assigned to $rowG, so I tried to hack meaning out of this from the code's errors and came up with that :

<?php
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
?>
    <tr>
        <td><?php echo $rowG[2]; ?></td>
        <td><?php echo $rowG[1]; ?></td>
        <td><?php echo $rowG[0]; ?></td>
        <td><?php echo $rowG[3]; ?></td>
        <td><?php echo '<a href="./consultation.php?Login='.$rowG[2].'"> Link </a>'; ?></td>
    </tr>
<?php
}
?>

If it doesn't work with you, you'll have to provide the informations that should have been included in your question since the begining.

Sign up to request clarification or add additional context in comments.

6 Comments

What is assigned to $rowG ? The values that are in my DB and I get the results from my query. Do you also need the query ? For example $rowG[0] is an adress and $rowG[1] is a first name etc... Do you need more info ? I would like to echo the values from my query. Without loop it works but with a loop only the first entry is printed;
Why don't you use the string keys that your query returns instead of indexes ? Did you tried my code ?
Yes I tried it but nothing happen, No results printed. I had already tried this earlier in the day. thank you
@Rflow You know what to do, then.
I have used string keys but same result. I don't really know what to do.
|
1

This is basic iteration over query results:

while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {?>
<tr>
    <td><?php echo $rowG[2]; ?></td>
    <td><?php echo $rowG[1]; ?></td>
    <td><?php echo $rowG[0]; ?></td>
    <td><?php echo $rowG[3]?></td>
    <td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
</tr>
<?php
}

5 Comments

<td><?php echo "<a href='./consultation.php?Login={$rowG[2]}'> Link </a>" ; ?></td>. I like to add curly braces around interpolated variables.
@Spectarion sometimes this definitely can help)
Yes. Especially if you need to add a prefix or suffix. Example: {$logged_username}s profile => u_mulders profile.
I know I tried it but I'm using <th> and it seems that is not working it print not well formated table.
th used in thead only. What do you mean by not well formated? You can edit your question and give more info, even pic of this table.
1
<?php foreach($array as $rowG=>$value): ?> <tr>
                            <td><?php echo $rowG[2]; ?></td>
                            <td><?php echo $rowG[1]; ?></td>
                            <td><?php echo $rowG[0]; ?></td>
                            <td><?php echo $rowG[3]?></td>
                            <td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>

            <?php endforeach;}} ?>

Would be :

<?php foreach($rowG as $arr): ?> <tr>
                            <td><?php echo $arr[2]; ?></td>
                            <td><?php echo $arr[1]; ?></td>
                            <td><?php echo $arr[0]; ?></td>
                            <td><?php echo $arr[3]?></td>
                            <td><?php echo "<a href='./consultation.php?Login=$arr[2]'> Link </a>" ; ?></td>

            <?php endforeach;}} ?>

Note the use of $arr instead of $rowG.
In the original code $array is not used.

2 Comments

Thank you. I tried it but I have now 10 duplicate entries. Do you know how to fix it ?
Try the query in mysql. Review the code for an outer loop.

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.