check if database row is empty - php

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']!='')

Related

Number auto increase with other while function (PHP)

I want the queqe id auto increase start from 1
I have an mysql table call t1
mysql table t1 Data as below:
+----------+------------------+-------------+
| ID | Name | Status |
+----------+------------------+-------------+
| 1 | ABBCCC | 1 |
| 2 | BASDASD | 1 |
| 3 | ABBCCC | 1 |
| 4 | ABBCCC | 2 |
+-------------------------------------------+
I loop data in php like this:
$quserCA = DB::query("SELECT * FROM ".DB::table('jnbook_book')." WHERE Name = 'ABBCCC' ORDER BY id DESC LIMIT 20");
$nqCA = mysql_num_rows($quserCA);
while($ruserCA = DB::fetch($quserCA)){
$CAlist[] = $ruserCA;
}
$x = 1;
while($x <= $nqCA) {
//echo "The number is: $x <br>";
$x++;
}
I loop this in my htm like this:
<table>
<tr>
<td>Queqe ID</td><td>ID</td><td>Status</td>
</tr>
<!--{loop $CAlist $value}-->
<tr>
<td>{$x}</td><td>{$value[id]}</td><td>{$value[status]}</td>
</tr>
<!--{/loop}-->
</table>
But after that my table output as below show
+---------------+-------------------+----------------+
| Queqe ID | ID | Status |
+---------------+-------------------+----------------+
| 1 | 1 | 1 |
| 1 | 3 | 1 |
| 1 | 4 | 2 |
+----------------------------------------------------+
Actually what I want the table output as below
(I want the queqe id auto increase start from 1):
+----------+-----------------+-----------------+
| Queqe ID | ID | Status |
+----------+-----------------+-----------------+
| 1 | 1 | 1 |
| 2 | 3 | 1 |
| 3 | 4 | 2 |
+----------------------------------------------+
Thank you.
This should be done something like:
$x = 1;
while($ruserCA = DB::fetch($quserCA)){
// add a field, say `x` with number of a record:
$ruserCA['x'] = $x++;
$CAlist[] = $ruserCA;
}
In a template:
<td>{$value[x]}</td><td>{$value[id]}</td><td>{$value[status]}</td>

display table group by category using php

my database design is like this
-- Table Reason
--------------------------
| reasonid | reasonname |
--------------------------
| 1 | reason1 |
| 2 | reason2 |
--------------------------
-- Table Student
----------------------------
| studentid | studentname |
----------------------------
| 1 | John |
| 2 | Jane |
| 3 | Hulk |
----------------------------
-- Table form
-----------------------------------
| formid | studentid | reasonid |
-----------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 1 |
-----------------------------------
I want to show data table like :
reason1
| 1 | John |
| 2 | Hulk |
reason2
| 1 | Jane |
I have tried below code but the result is not groupBy reason
<?php $i =1;
while($rowfet = mysql_fetch_array($myselect)){ ?>
<h2><?php echo $rowfet['ReasonName']; ?></h2>
<table>
<thead>
<tr class="active">
<th>No.</th>
<th>StudentName</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><?php echo $i; ?></td>
<td><?php echo $rowfet['STUDENTNAME']; ?></td>
</tr>
</tbody>
</table>
<?php $i++; } ?>
the result of this code is :
reason1
| 1 | John |
reason1
| 2 | Hulk |
reason2
| 3 | Jane |
You could do something like this:
outside while loop:
$lastReasonId = '';
inside while loop:
if($rowfet['reasonid'] != $lastReasonId){
// show heading
}
$lastReasonId = $rowfet['reasonid'];
But your code is far from ok. The $i++ thing is not doing anything, you should probably echo $rowfet['studentid'] there.
And you are using deprecated mysql code.

Replace empty row using Mysql With 'IN' condition

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>";
}

PHP: Fill fixed size chart with mysql data

I have a table chart. Lets say 5 by 5. I run a loop such as
<table>
<tbody>
<?php for ($i = 0; $i < 5; $i += 5) {
echo "<tr>
<td>one box</td>
<td>one two</td>
<td>one three</td>
<td>one four</td>
<tr>";
}?>
</tbody>
</table>
It creates a table such as
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
Now I have mysql data I load for my purposes and I need it to put the data in respectively so the table looks like
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | Res 1| | | |
-------------------------------------
| | | | Res 3| |
-------------------------------------
| |Res 4 | | | Res 2|
-------------------------------------
How would I do this? I have 50 results and need to fill the results into the correct column and row. I need to do some sort of if(results[0-50]['id'] == rowcolumnid) echo the results for the correct table while doing the for loop.
Edit: Here is my full code.
<table id="schedule">
<tbody>
<?php
$time = mktime(0, 0, 0, 1, 1);
for ($i = 28800; $i < 62200; $i += 1800) { ?>
<tr id="row<?php echo $i; ?>">
<td id="hour">
<?php
printf('%1$s',date('g:i a', $time + $i));
?>
</td>
<td id="sunday"></td>
<td id="monday"></td>
<td id="tuesday"></td>
<td id="thursday"></td>
<td id="friday"></td>
<td id="saturday"></td>
</tr>
<?php } ?>
</tbody>
</table>
My mysql results are in datetime format that I'm going to use to propogate the table.
Results:
ID| EVENT NAME | DATEOFEVENT
1 | event name | 2012-11-20 12:00:00
2 | event name | 2012-11-21 13:30:00
3 | event name | 2012-11-22 13:00:00
4 | event name | 2012-11-23 11:00:00
5 | event name | 2012-11-24 08:00:00
etc.
I can do a strtotime of the dates and a date command to match.
If you first fetch the data (or if it is sorted if you first fetch the first data), then you can just iterate over the data when you match the hour/time that you iterate over to draw the table.
As an example, I've chosen to display only one column that represents 5 hours (1-5) of which some can be matched. Those that are matched, are stored in an array and made available as an iterator (ArrayIterator):
$data = [3,5];
$datas = new ArrayIterator($data);
$datas->rewind();
Matched hours are represented with 1, unmatched ones with 0:
echo "+---+---+\n";
foreach(range(1, 5) as $hour)
{
if ($hasData = ($datas->valid() and $datas->current() === $hour)) {
$datas->next();
}
$hasData = (int) $hasData;
echo "| $hour | $hasData |\n";
echo "+---+---+\n";
};
Output:
+---+---+
| 1 | 0 |
+---+---+
| 2 | 0 |
+---+---+
| 3 | 1 |
+---+---+
| 4 | 0 |
+---+---+
| 5 | 1 |
+---+---+
This works perfectly if the data from the data is available as an iterator (often the case, for mysql_* you need to write you one) and if it is sorted.
Even this is only a single list here, for a table this works actually equally because a table is just a different form of representing the data.

Need help ordering data from mysql_fetch_array()

I'm having a hard time organizing the data that I get from mysql_fetch_array().
I have a DB table with that looks something like this:
+---------------------+------------------+---------------+--------+---------+
| date | name | indexed_pages | nameID | entryID |
+---------------------+------------------+---------------+--------+---------+
| 2012-06-15 21:18:06 | site1.com | 200 | 1 | 1 |
| 2012-06-15 21:18:10 | site2.com | 25 | 2 | 1 |
| 2012-06-15 21:18:13 | site3.com | 12 | 3 | 1 |
| 2012-06-15 21:18:16 | site4.com | 8 | 4 | 1 |
| 2012-06-15 21:18:19 | site5.com | 2 | 5 | 1 |
| 2012-06-16 00:11:12 | site1.com | 191 | 1 | 2 |
| 2012-06-16 00:11:21 | site2.com | 25 | 2 | 2 |
| 2012-06-16 00:11:30 | site3.com | 12 | 3 | 2 |
| 2012-06-16 00:11:44 | site4.com | 8 | 4 | 2 |
| 2012-06-16 00:11:51 | site5.com | 2 | 5 | 2 |
| 2012-06-18 10:20:47 | site1.com | 191 | 1 | 3 |
| 2012-06-18 10:20:52 | site2.com | 25 | 2 | 3 |
| 2012-06-18 10:20:56 | site3.com | 12 | 3 | 3 |
| 2012-06-18 10:21:00 | site4.com | 8 | 4 | 3 |
| 2012-06-18 10:21:04 | site5.com | 2 | 5 | 3 |
+---------------------+------------------+---------------+--------+---------+
I need to order the results in a Google Line Graph in the following manner:
['date', 'site1_entryID=1', 'site2_entryID=2', 'site3_entryID=3', (...)],";
The thing is that I'm having trouble managing the arrays that I generate. I'm using the following code:
mysql_connect("host_here", "username_here", "pass_here") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());
$query = "SELECT * FROM pages";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
After this I need to echo the number of indexed_pages for each site where entryID = 1.
I don't know if this description is confusing or not, but I've tried pretty much everything and can't get the organize the data from the arrays to serve what I need to do. Help, please!
Thanks in advance!
Don't use select *, that's lazy, and you're stuck accepting the fields in the order the DB decides to produce them in.
Specify the fields you want, in the order you want:
SELECT date, name, indexed_pages, etc...
I think the simplest query is :
$result= mysql_query("SELECT name, index_pages, entryID from table_name WHERE entryID =
1");
while($row=mysql_fetch_array($result)){
echo "$row[name]";
echo "$row[index_pages]";
echo "$row[entryID]";
}
Try this. There might be some mistakes. Because i developed it fast. And replace table_name with yours.
Or you can display it in a table:
echo "<table>";
echo "<tr><td>Sit Name</td>";
echo "<td>Page Name</td>";
echo "<td>EntryID</td>";
echo "</tr>";
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>$row[name]</td>";
echo "<td>$row[index_pages]</td>";
echo "<td>$row[entryID]</td>";
echo "</tr>";
}
echo "</table>";
SELECT date, name, indexed_pages
FROM pages
where entryID=1
order by date asc ,name asc
Not sure if this will help
mysql_connect("host_here", "username_here", "pass_here") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());
$query = "SELECT * FROM pages";
$result = mysql_query($query);
$data[]='date';
while($row = mysql_fetch_assoc($result)){
$name=substr($row['name'], -4);
$data[]= $name."_entryID=".$row['entryID'];
}
A little of a brute force method.

Categories