This is my code
$Qemaster="select * from emaster where `branch`='$bid' and `department`='$did' and `status`!='L'";
$Remaster=mysql_query($Qemaster);
while($Rowemaster=mysql_fetch_array($Remaster)){
$empcode=$Rowemaster[id];
$name=$Rowemaster[name];
$Tleave=0;
echo "<tr>";
echo "<td rowspan='2'>".$name."</td>";
echo "<td>Leave</td>";
$Qlp="select `leave` from lpsummary where ((`month` IN(04,05,06,07,08,09,10,11,12) and `year`='$year') or (`month` IN(01,02,03) and `year`='$Nyear')) and `empcode`='$empcode'";
$Rlp=mysql_query($Qlp);
while($Rowlp=mysql_fetch_array($Rlp)){
$leave=$Rowlp['leave'];
$Tleave=$Tleave+$leave;
echo "<td>".$leave."</td>";
}
echo "<td><font color='red'>".$Tleave."</font></td>";
echo "<tr><td>Percentage</td>";
}
and my table is
------------------------------------------
| name | apr-12 | may-12 | jun-12 | jul-12 |
|------|--------|--------|--------|--------|
|Kumar | 2 | 1 | 0 | 3 |
|Rajan | 4 | 0 | 2 | |
| | | | | |
|------------------------------------------
Here under the name Rajan there is no data in jun-12 but jul-12 had the value 2...ie)empty row in the table lpsummary ...... if there is empty i wanna to replace it with as '-'... How can i do that by my code.....
In your while loop, you need to put a condition to check if a null from was returned.
while($Rowlp=mysql_fetch_array($Rlp)){
if (is_null($Rowlp['leave'])) {
$leave = '-';
} else {
$leave=$Rowlp['leave'];
$Tleave=$Tleave+$leave;
}
echo "<td>".$leave."</td>";
}
Related
In my program, the following query and code generate the following table like this:
+-------+---------+
|ward_id|Sub_block|
+-------+---------+
| 1 | A1 |
+-------+---------+
| 1 | B1 |
+-------+---------+
| 1 | C1 |
+-------+---------+
| 2 | D1 |
+-------+---------+
| 2 | E1 |
+-------+---------+
| 2 | F2 |
+-------+---------+
| 3 | K1 |
+-------+---------+
| 3 | G2 |
+-------+---------+
| 3 | I3 |
+-------+---------+
Here is my code that generate the above table.
if(isset($_POST["union_id"]) && !empty($_POST["union_id"]))
{
//Get all union data
$query = $mysqli->query("SELECT * FROM test_epi_table WHERE union_id = ".$_POST['union_id']);
//Count total number of rows
$rowCount = $query->num_rows;
//Display unions list
if($rowCount > 0)
{
echo "<hr>";
echo "<h3 align='center'>EPI Schedule</h3>";
echo "<table class='table table-bordered'>
<tr>
<th>Ward No</th>
<th>Sub-Block</th>
</tr>";
while($row = $query->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['ward_no'] . "</td>";
echo "<td>" . $row['sub_block_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<hr>";
}
else
{
echo "<br>";
echo "<h3 align='center'>Sorry, No Available Information!</h3>";
echo "<hr>";
}
}
Now I like to create the table like this. Mostly I think it is a rowspan task.
+-------+---------+
|ward_id|Sub_block|
+-------+---------+
| | A1 |
+ +---------+
| 1 | B1 |
+ +---------+
| | C1 |
+-------+---------+
| | D1 |
+ +---------+
| 2 | E1 |
+ +---------+
| | F2 |
+-------+---------+
| | K1 |
+ +---------+
| 3 | G2 |
+ +---------+
| | I3 |
+-------+---------+
what kind of changes do I need to add in my actual php code? Please help me out. Thanks in advance.
Use Inner query is one of the good way also you can use right outer join if you want it to be more efficient.
I wanted to check if the next TWO rows or ID is not NULL.
because if there is no succeeding TWO rows or ID then the income remains zero.
foreach ($data as $row)
{
if(($row->id + 2) != NULL) //I don't know what is the correct statement here
{
echo "<tr>";
echo "<td>".$row->id."</td>";
echo "<td>".$row->username."</td>";
echo "<td>"."650.00"."</td>";
echo "<td>".$row->remarks."</td>";
echo "<tr>";
}
else
{
echo "<tr>";
echo "<td>".$row->id."</td>";
echo "<td>".$row->username."</td>";
echo "<td>".$row->income."</td>";
echo "<td>".$row->remarks."</td>";
echo "<tr>";
}
}
Here is the table I want to achieve.
==================================
|ID | Username | Income | Remarks|
| 2 | user1 | 650.00 | |
| 3 | user2 | 650.00 | |
| 4 | user3 | 0.00 | |
| 5 | user4 | 0.00 | |
==================================
If I add a username then the next output will be this:
==================================
|ID | Username | Income | Remarks|
| 2 | user1 | 650.00 | |
| 3 | user2 | 650.00 | |
| 4 | user3 | 650.00 | |
| 5 | user4 | 0.00 | |
| 6 | user5 | 0.00 | |
==================================
You need to change your foreach to get the index.
foreach ($data as $index => $row)
Then you can address all rows relative to your current row with:
$row_two_ahead = $data[$index + 2];
you should check however if that row exists before you try to use it or you will get index out of range exceptions:
if (isset($data[$index + 2])) {
}
I solved the problem...
this is the code from my controller:
public function addUsername()
{
$data['id'] = NULL;
$data['username'] = $this->input->post('username');
$data['reward'] = 0.00;
$data['remarks'] = ' ';
$this->Matrix_model->addUsername($data);
$last = $this->Matrix_model->getLast();
$index = $last - 2; //I minus the last index with two
$this->Matrix_model->updateIncome($index); //and updated the income of that index
redirect('http://localhost/matrix/index.php/matrix');
}
and this is the code from my model:
public function addUsername($data)
{
$this->db->insert("income", $data);
}
public function getLast()
{
return $this->db->insert_id(); //this is the method to access the last id inserted
}
public function updateIncome($id)
{
$this->db->set("reward", 650);
$this->db->where("id", $id);
$this->db->update("income");
}
thank you and I'm sorry if other programmers didn't understand my questions
I have a database wich looks like this:
+----------+----------+---------+----------+
| bogie_id | train_id | axle_nr | bogie_nr |
+----------+----------+---------+----------+
| 1 | 1 | 1 | |
| 2 | 1 | 2 | |
| 3 | 1 | 3 | |
| 4 | 1 | 4 | |
+----------+----------+---------+----------+
As you can see the "bogie_nr" rows are empty.
No i wana make an if statement, wich checks if the rows are empty or filled in.
What i currently have:
<div id="bogiebox">
<tr>
<?php
$x = 1;
foreach($show_axle as $bogiebox){ ?>
<input type='hidden' name='bogie_id[<?php echo $bogiebox['bogie_id']?>]' value='<?php echo $bogiebox['bogie_id']?>'>
<td>
<?php
if($bogiebox['bogie_nr'] == ''){
?>
<input type='checkbox' id="bogie_axle_fields" value="<?= $x ?>" name='bogie_nr[<?php echo $bogiebox['bogie_id']?>]'></td>
<?php
}
else{
?>
<input type='checkbox' id="bogie_axle_fields" checked disabled></td><?php } }
?>
</tr>
</div>
And then the if statement:
<?php
if(isset($_POST['y'])){
$bogiefilledin = false;
if(!empty($_POST['bogie_nr'])) {
echo "everything is full?";
}
else {
echo "Fill in some more fields";
}
}
?>
It is not done yet! but what it should do is:
We check a checkbox or 2. press the send button, and the database inserts the number 1 for the 2 selected bogies. Then the page refeshes and all the checkboxes should have a value of 2. And on the next insert they will insert the value 2. and so on and so on.
But, what i would like = if i filled in my database like this:
+----------+----------+---------+----------+
| bogie_id | train_id | axle_nr | bogie_nr |
+----------+----------+---------+----------+
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 1 |
| 3 | 1 | 3 | 2 |
| 4 | 1 | 4 | |
+----------+----------+---------+----------+
I would like to see the else statement (Fill in some more fields). because the 4th axle is not in a bogie yet.
Right now the if else statement shows me: when i select 1 and press submit it says: "everything is full!" . and when i select nothing it says: "Fill in some more fields.". However it does not insert anything yet, so it should always say: Fill in some more fields!.
How do i do this??
try changing if(!empty($_POST['bogie_nr'])) to this if($_POST['bogie_nr']!='')
Goal
I'm trying to draw up a restaurant menu in HTML using MySQL queries.
MySQL Tables
Categories
+-----------------------------------+
| id | nom_categorie | dimensions |
+-----------------------------------+
| 1 | pâtes | 1-2 |
| 2 | ailes de poulet | 3-4-5 |
+-----------------------------------+
Dimensions
+---------------------+
| id | dimension |
+---------------------+
| 1 | Petit |
| 2 | Gros |
| 3 | Unité |
| 4 | Repas (6) |
| 5 | Repas (12) |
+---------------------+
Repas
+-----------------------------------+
| id | repas | prix |
+-----------------------------------+
| 1 | spaghetti | 8.75,11.75 |
| 2 | lasagne | 9.95,13.25 |
| 3 | régulières | 0.95,9.50,11.95 |
| 4 | piquantes | 0.95,9.50,11.95 |
+-----------------------------------+
Desired outcome
+--------------------------------------+
| Pâtes |
+--------------------------------------+
| | petit | gros |
+--------------------------------------+
| spaghetti | 8.75 | 11.75 |
| lasagne | 9.95 | 13.25 |
+--------------------------------------+
+-----------------------------------------------+
| Ailes de poulet |
+-----------------------------------------------+
| | Unité | Repas (6) | Repas (12) |
+-----------------------------------------------+
| Régulières | 0.95 | 9.50 | 11.95 |
| Piquantes | 0.95 | 9.50 | 11.95 |
+-----------------------------------------------+
Current outcome
+--------------------------------------+
| Pâtes |
+--------------------------------------+
| | 1 | 2 |
+--------------------------------------+
| spaghetti | 8.75 | 11.75 |
| lasagne | 9.95 | 13.25 |
+--------------------------------------+
+--------------------------------------+
| Ailes de poulet |
+--------------------------------------+
| | 3 | 4 | 5 |
+--------------------------------------+
| Régulières | 0.95 | 9.50 | 11.95 |
| Piquantes | 0.95 | 9.50 | 11.95 |
+--------------------------------------+
Current code
function catalogue_complet($mysqli) {
$categories = mysqli_query($mysqli, "SELECT * FROM categories ORDER BY nom_categorie ASC");
$produits = mysqli_query($mysqli, "SELECT * FROM produits ORDER BY id, prix ASC");
$dimensions = mysqli_query($mysqli, "SELECT * FROM dimensions");
while($categorie = mysqli_fetch_array($categories))
{
echo "<h2>".$categorie[1]."</h2>";
echo "<table class='catalogue'>";
if(!empty($categorie[2]))
{
echo "<tr>";
echo "<th> </th>";
$array_categorie = explode("-",$categorie[2]);
foreach($array_categorie as $categorie)
{
echo "<th>".$categorie."</th>";
}
echo "</tr>";
}
while($produit = mysqli_fetch_array($produits))
{
if($categorie[0] == $produit[2])
{
echo "<tr>";
echo "<td>".$produit[1]."</td>";
$array_prix = explode(",",$produit[2]);
foreach($array_prix as $prix)
{
echo "<td class='prix'>".$prix."</td>";
}
echo "</tr>";
}
}
echo "</table>";
}
}
Other details
I may have a few errors slipped in with keys (example: $produit[2]) as I've simplified the tables, but I do have the proper information in my output. As I've mentioned above, my problem lies in the fact that I get the dimension ID as entered in my table repas. I can't figure out how to get the proper information (Ailes de poulet and Pâtes) to show up ?
I think you basically need to get the column names of your tables correctly.
In your current code, you are not using the dimensions table at all, although you have queried it.
Keep the names of all dimensions in an array, and refer them whenever the id is known.
Storing the dimensions in an array.
$dimensions = mysqli_query($mysqli, "SELECT * FROM dimensions");
$dim_arr = Array();
if($dimensions){
while($d = $mysqli_fetch_assoc($dimensions)){
$dim_arr[$d['id']] = $d['dimension'];
}
}else{
die(mysqli_error($mysqli));
}
Using them inside the foreach loop.
foreach($array_categorie as $categorie)
{
//Perform error checking here - if(isset($dim_arr[$categorie]))
$dim_name = $dim_arr[$categorie];
echo "<th>".$dim_name."</th>";
}
This should display the headings.
Ok, so I've found a working solution. Please excuse the length of the code, and as mentioned in the original post, some differences may occur when looking at the tables in the question, but it is simplified to focus on the desired effect.
Bottom line, the code below produces the table with title (ex: Pâtes), and each product and price line (ex: Lasagne | 9,95 | 13,25)
function catalogue_complet($mysqli) {
$categories = mysqli_query($mysqli, "SELECT * FROM catalogue_categories ORDER BY nom_categorie ASC");
$produits = mysqli_query($mysqli, "SELECT * FROM produits ORDER BY id, prix ASC");
$dimensions = mysqli_query($mysqli, "SELECT * FROM dimensions");
while($categorie = mysqli_fetch_array($categories))
{
echo "<h2>".$categorie[1]."</h2>";
echo "<table class='catalogue'>";
mysqli_data_seek($produits, 0);
if(empty($produits))
{
echo "<tr><td>".AUCUN_PRODUIT."</td></tr>";
}
else
{
while($produit = mysqli_fetch_array($produits))
{
// Dimensions -----------------------------------------------------------
if(!empty($categorie[3]))
{
echo "<tr>";
while($dimension = mysqli_fetch_array($dimensions))
{
$array_dimensions = explode("-",$categorie[3]);
foreach($array_dimensions as $dim)
{
if($dim == $dimension[0])
{
if($i == 0)
{
echo "<th> </th>";
}
echo "<th>".$dimension[1]."</th>";
$i++;
}
}
}
echo "</tr>";
}
// Produits -------------------------------------------------------------
if($categorie[0] == $produit[4])
{
echo "<tr>";
echo "<td>".$produit[1]."</td>";
$array_prix = explode(",",$produit[3]);
foreach($array_prix as $prix)
{
if( empty($prix) or ($prix == "0"))
{
echo "<td> </td>";
}
else
{
echo "<td class='prix'>".str_replace(".",",",$prix)." $</td>";
}
}
echo "</tr>";
}
}
}
echo "</table>";
}
}
Table
+-----+--------+---------+
| ID | Name | Images |
+-----+--------+---------+
| 001 | John | 5 |
| 002 | Mark | 3 |
+-----+--------+---------+
i would like to display like this
Jon, 001-1.jpg | 001-2.jpg | 001-2.jpg | 001-3.jpg | 001-4.jpg | 001-5.jpg |
Mark, 002-1.jpg | 002-2.jpg | 002-2.jpg | 002-3.jpg |
the images value on database table will be the number of images return to create images link
You can use this though. Didn't know why you found it difficult.
$c = 0
while (false !== ($data = fetch_array_as_row_function()))
{
echo "<tr>";
echo "<td>", $data["name"], "</td>";
for ($i = 0; $i < $data["images"]; $i++)
echo "<td>00", $c,"-", $i, "</td>";
echo "</tr>";
}
Here, the function fetch_array_as_row_function() is something equivalent to what mysql_fetch_array() does.