So here is what I did so far: http://d.pr/i/c6z
Code:
<tbody>
<?php while ($row = mysql_fetch_assoc($result)): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<?php foreach ($row as $key): ?>
<td><a href="#"><?php echo $key; ?></a></td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</tbody>
and my mysql table looks like that: id (PRIMARY KEY), fullname, username. As you can see, what i'm trying to do is to display all of these records in html table, but what I don't want is href link in first column, just a numbers. SSo, how to remove in foreach loop the first value from the array which is 'id' or maybe there is a better way to do this thing?
try this :
<tbody>
<?php while ($row = mysql_fetch_assoc($result)): ?>
<tr>
<?php foreach ($row as $key): ?>
<?php if($key === 'id'): ?>
<td><?php echo $row['id']; ?></td>
<?php else: ?>
<td><a href="#"><?php echo $key; ?></a></td>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</tbody>
Two different views so you have two conditions. It is easier to read for a view.
<tbody>
<?php while ($row = mysql_fetch_assoc($result)): ?>
<tr>
<td><?php echo $row['id']; unset($row['id']); ?></td>
<?php foreach ($row as $key): ?>
<td><a href="#"><?php echo $key; ?></a></td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</tbody>
Without commenting on the efficiency or otherwise of your code, you can always use unset
to unset a specific element of your array, for example,
unset($row['id']);
will unset the element of $row
array with key id
- effectively removing that element from the array.