I have an html webpage, and it contains a unordered list .So i want to fill this list using items that are stored in a MySQL database, an example item is provided to be clear.
<ul class="content">
<li class="class1">
<p id="id1">paragraph1</p>
<div class="class1sub">
<p>paragraph1sub</p>
</div>
</li>
So i want to retrieve the whole list items from the database and display it on the webpage. The problem is that each list item is stored in the database besides its CSS classes and IDs to be styled. Here is an example of a couple of items:
liclass - id - paragraph1 - divclass - paragraph2
So I'm thinking of maybe using JavaScript/PHP and MySQL ... but no starting code on my mind...any help ?
I have an html webpage, and it contains a unordered list .So i want to fill this list using items that are stored in a MySQL database, an example item is provided to be clear.
<ul class="content">
<li class="class1">
<p id="id1">paragraph1</p>
<div class="class1sub">
<p>paragraph1sub</p>
</div>
</li>
So i want to retrieve the whole list items from the database and display it on the webpage. The problem is that each list item is stored in the database besides its CSS classes and IDs to be styled. Here is an example of a couple of items:
liclass - id - paragraph1 - divclass - paragraph2
So I'm thinking of maybe using JavaScript/PHP and MySQL ... but no starting code on my mind...any help ?
Share Improve this question edited Mar 14, 2015 at 12:22 Paweł Tomkiel 1,9842 gold badges21 silver badges39 bronze badges asked Sep 17, 2011 at 21:28 oudouzoudouz 1052 gold badges3 silver badges11 bronze badges 3- 2 This is very basic PHP, you should be able to find a tutorial to help you. – JohnD Commented Sep 17, 2011 at 21:44
-
1
@rwilliams That uses
mysql_*
functions. Bad. – JohnD Commented Sep 17, 2011 at 21:45 - @johnD: I deleted the link. I just wanted to attempt to get the OP thinking for themselves and not just asking for the code. – rwilliams Commented Sep 17, 2011 at 22:13
2 Answers
Reset to default 6PHP is definitely the way to get this done. I've used the CodeIgniter php framework in a lot of my projects. It's very easy to set up and use, but feel freed to shop around to find what's best for you. I'll try not to be too CodeIgniter specific here, but if you do go this route you can check out this video on basic database function with CodeIgniter.
Basically whatever you choose to do there are going to have few basic parts. Some config area where you type in you Database name, and username and password.
Example Database Config file:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "myusername";
$db['default']['password'] = "mypassword";
$db['default']['database'] = "mydatabse";
$db['default']['dbdriver'] = "mysql";
Now that the php knows how to talk to your database it's time to get the data and send it to be displayed.
This next part is more specific to CodeIgniter, mostly because that's what I have the most experience with, but many of the other frameworks will be something similar. The next three code snips will be represent three different files. The idea is to abstract/remove the backend database stuff from the front end content. This is referred to as Model-View-Controller(MVC)
Run your query (Model):
function getData()
{
$query = $this->db->get('myTable');
return $query->result();
}
Set that data and send it to your view(Contorller):
function addReceipt()
{
$data = array();
if($query = $this->my_model->getData())
{
$data['content'] = $query;
}
$this->load->view('my_view', $data);
}
Lastly display your data by looping through records and using php echo(View):
<ul class="content">
<?php if(isset($content)) : foreach($content as $row) : ?>
<li class="<?php echo $row->liclass; ?>">
<p id="<?php echo $row->ID; ?>"><?php echo $row->paragraph1; ?></p>
<div class="<?php echo $row->divclass; ?>">
<p><?php echo $row->paragraph2; ?></p>
</div>
</li>
<?php endforeach; ?>
</ul>
This would be interesting if you showed your php code here: but this is what you should do
<?php
mysql_connect('hostname','username','password');
mysql_select_db('dbname');
$sql = "select *from tablename";
$run = mysql_query($sql);
echo "<table>";
echo "<tr><th>NAME</th><th>SURNAME</th><th>GENDER</th><th>BIRTHDAY</th></tr>";
$i = 1;
while($row = mysql_fetch_array($run)){
if($i%2 == 0) $bgcolor = 'lightblue';else $bgcolor = 'white';
$i++;
echo "<tr style='backgroundcolor".$bgcolor."'><td>NAME</td><td>".$row['SURNAME']."</td><td>".$row['GENDER']."</td><td>".$row['BIRTHDAY']."</td></tr>";
}
echo "</table>";
?>