2

Ive got a pretty basic table named 'customers' with four columns:
ID (primary Auto Increment)
businessName
contactName
contactEmail

I call it with:

$result = mysqli_query($con, "SELECT * FROM customers");

and was using mysqli_fetch_array to display it on the page with a foreach loop:

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{    
    echo "<tr>";
    foreach ($row as $value) {
        echo "<td>" . $value . "</td>";
    }
    echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
    echo "</tr>";
}

Which gives you an array like:

Array ( [id] => 1 [businessName] => Microsoft [contactName] => Bill Gates [contactEmail] => [email protected] ) Array ( [id] => 2 [businessName] => Amazon [contactName] => Jeff Bezos [contactEmail] => [email protected] ) 

Is it possible to display results differently based on which column (technically now position in the array) they are in? I would like to make a

a href="mailto:[email protected]"  

link when it gets to contactEmail.
What if I wanted to display just one key or value instead of using foreach?
It doesn't seem possible to call

$row['contactEmail'] 

in the foreach loop which confuses me since below that I am able to create a link to

$row['id']

If anyone has any ideas how to do this, or if there is a better way to be displaying this information, I'm open to suggestions, as I'm not very good at programming, especially PHP.

4
  • 1
    $row['contactEmail'] should work just fine. What error are you getting? Commented Nov 17, 2016 at 17:23
  • Check that you're writing 'contactEmail' exactly as it's stored in database. It's case sensitive according to documentation. Commented Nov 17, 2016 at 17:23
  • Also you can try mysqli_fetch_assoc() link which will definitely get you an array you can access elements in. Commented Nov 17, 2016 at 17:24
  • I'd probably get rid of that inner foreach, as you only have 4 columns - it'll be clearer to just explicitly write out what each column you're trying to display. Commented Nov 17, 2016 at 17:30

2 Answers 2

5

Yes! you can just add like this:

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{    
    echo "<tr>";
    foreach ($row as $key => $value) {
        if($key == "contactEmail"){
          $mailUrl = "mailto:".$value;
          echo "<td><a href=".$mailUrl.">".$value."</a>";
        }
        else{
          echo "<td>" . $value . "</td>";
        }
    }
    echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
    echo "</tr>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, You can able to mysqli_fetch_array retrieve data directly using foreach loop. like bellow :

<?php
  $result = mysqli_query($con, "SELECT * FROM customers");
?>
<table>
    <tr>
        <td>Business Name</td>
        <td>Contact Name</td>
        <td>#</td>
    </tr>
    <?php
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {    
        echo "<tr>";
            echo "<td>".$row['businessName']."</td>";       
            foreach ($row as $key => $value) {
                $newValue = $key == "contactEmail" ? '<a href="mailto:'.$mailUrl.'">'.$value.'</a>' : $value;
                echo "<td>" . $newValue . "</td>";         
            }
            echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
        echo "</tr>";
    } 
    ?>
</table>

Comments

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.