I'm an interaction designer building out front_end HTML design. I am new to PHP, but old hand with HTML and quick to new technology. I'm hoping someone can help me out. I've done a few hours of research and can't find the answer to in any forum so far to my very specific problem.
GOAL: I'd like a nice way to display various discrete cells of data from my database into my HTML designs. Specifically, I'd like to be able to display a given 'title' on my page design by referencing a unique id in the same row of the database. Here's what one row looks like...
'unique_id' 'title' 'description' 'image'
What I need is a simple way to pull individual cells from an array using some small code snippet. Something like this:
<li class="active"><a href="#"><?php echo [unique_id][title];?></a></li>
...where unique_id would work as an index that would tell the array which row to look at, and then display the title from that same row.
I have a hunch it has to do with indexed arrays and some special loops, but I am very lost. Your help, but more importantly teaching me so I can learn, would be most appreciated.
Here's what I have currently, but it doesn't work because it's only going off the incremental row order (0..., 1..., 2...). What I need is for those numbers to be unique_ids for the rows, like (1005..., 104..., 106...).
<?php
$sql = "SELECT title FROM phones";
$result = mysql_query($sql) or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
?>
<ul class="nav nav-list">
<li class="nav-header">iOS Phones</li>
<li class="active"><a href="#"><?php echo $array[0][title];?></a></li>
<li><a href="#"><?php echo $array[1][title]?></a></li>
Christopher
As far as I understand, you wish to list the titles along with their unique ID's?
It can be achieved using MySQL & PHP like this:
<?php
$sql = "SELECT * FROM phones";
$result = mysqli_query($sql);
echo "<ul>";
while ($row = mysqli_fetch_array($result)) {
echo "<li><a href='#'>" . $row["unique_id"] . " - " . $row["title"] . "</a></li>";
}
echo "</ul>";
?>
This will output something like this:
<ul>
<li><a href='#'>100 - HelloWorld</a></li>
<li><a href='#'>101 - AnotherTitle</a></li>
</ul>
And so forth.. You can order the outputs alphabetically by title or by the unique ID's. This can be done by adding ORDER BY title
or ORDER BY unique_id
to the end of your $sql
variable.
What it does is just loop through all the database entries received from SELECT * FROM phones
. Every row will be put into a $row
array.
I hope this helped a bit.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.