image
Hello guys. I wanna ask how to minimize the second table?
because i wanna it just printed once no loop but i want all the value to compare with the first table
here is my code
<?php
$query1 = mysql_query("SELECT * FROM training_detail AS s JOIN subject AS t JOIN training AS u JOIN employee AS v WHERE v.id_employee = $id1 AND s.nik LIKE v.nik AND u.id_subject LIKE t.id_subject AND s.id_training LIKE u.id_training ");
$i=1;
while($row1 = mysql_fetch_array($query1))
{
$date = $row1['date'];
$subject1 = $row1['subject_name'];
?>
<table class="table table-bordered">
<tr>
A
<td class="table-bordered">No</td>
<td class="table-bordered">date</td>
<td class="table-bordered">subject</td>
<td class="table-bordered">subject no</td>
<td class="table-bordered">revision no</td>
<td class="table-bordered">Trainer</td>
<td class="table-bordered">Institution</td>
</tr>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo date("j/F/Y", strtotime($date)); ?></td>
<td class="table-bordered"><?php echo $subject1; ?></td>
<td class="table-bordered"><?php echo $row1['subject_no']; ?></td>
<td class="table-bordered"><?php echo $row1['revision_no']; ?></td>
<td class="table-bordered"><?php echo $row1['trainer']; ?></td>
<td class="table-bordered"><?php echo $row1['institution']; ?></td>
</tr>
</br>
<table class="table table-bordered">
<tr>
B
<td class="table-bordered">No</td>
<td class="table-bordered">subject</td>
<td class="table-bordered">subject name</td>
<td class="table-bordered">subject no</td>
</tr>
<?php
$query2 = mysql_query("SELECT * FROM header_job AS r JOIN subject AS q JOIN employee AS p WHERE q.id_subject LIKE r.id_header AND r.id_job LIKE p.id_job AND p.id_employee = $id1 ORDER BY q.id_subject ASC ");
$x=1;
while($row2 = mysql_fetch_array($query2))
{
$subject2 = $row2['subject_name'];
if (strcasecmp($subject1, $subject2) != 0)
{
?>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo $row2['subject'] ?></td>
<td class="table-bordered"><?php echo $subject2; ?></td>
<td class="table-bordered"><?php echo $row2['subject_no']; ?></td>
</tr>
<?php
}
$x++;
}
$i++;
}
?>
</table>
</table>
sorry about my pic's words is my own language
if you want to minimize the output
try to use limit
like so:
$query2 = mysql_query("SELECT * FROM header_job AS r JOIN subject AS q JOIN employee AS p WHERE q.id_subject LIKE r.id_header AND r.id_job LIKE p.id_job AND p.id_employee = $id1 ORDER BY q.id_subject ASC limit 1");
Related
i'm creating a table that receive orders from users, the C_ID will store in (StationaryCustomersInfo) table where the C_ID is unique, and the table (StationaryOrders) will store every item with the C_ID, the C_ID in (StationaryOrders) table is not unique because C_ID will repeated with every items belongs to that C_ID.
i want to display every orders by the C_ID where every orders for each customer will displayed with separate table.
$sql=mysqli_query($link, "SELECT Item, Personal, Department, Quantity, Color FROM StationaryOrders, StationaryCustomersInfo WHERE StationaryCustomersInfo.C_ID = StationaryOrders.C_ID");
?>
<table align="center" border='3' style="width:60%; line-height:30px; border-color: #020B58;">
<tr>
<th>Items</th>
<th>Personal</th>
<th>Department</th>
<th>Quantity</th>
<th>Color</th>
</tr>
<?php
if($sql){
while($row=mysqli_fetch_assoc($sql)){
?>
<tr>
<td style="text-align:center"><?php echo $row['Item']; ?></td>
<td style="text-align:center"><?php echo $row['Personal']; ?></td>
<td style="text-align:center"><?php echo $row['Department']; ?></td>
<td style="text-align:center"><?php echo $row['Quantity']; ?></td>
<td style="text-align:center"><?php echo $row['Color']; ?></td>
</tr>
<?php
}
}
?>
</table>
I expect to display each customers orders with separate table and button belongs to that table.
This can be done by adding in your query the C_ID and sort the result by C_ID. Then, in php, you can check if the previous fetch C_ID is the same than the current one.
In example :
// Let's factorise the building of the <table> header there to avoid duplication in code
function ShowTableHeader($cid)
{
?>
<table align="center" border='3' style="width:60%; line-height:30px; border-color: #020B58;">
<caption>Orders of user <?php echo $cid; ?> : </caption>
<tr>
<th>Items</th>
<th>Personal</th>
<th>Department</th>
<th>Quantity</th>
<th>Color</th>
</tr>
<?php
}
$query = "SELECT sci.C_ID, "
. "Item, "
. "Personal, "
. "Department, "
. "Quantity, "
. "Color "
."FROM StationaryOrders so "
."LEFT JOIN StationaryCustomersInfo sci "
."ON sci.C_ID = so.C_ID "
."ORDER BY sci.C_ID";
$sql = mysqli_query($link, $query);
if ($sql)
{
if ($row=mysqli_fetch_assoc($sql))
{
// initialize the first C_ID for the first result
$previousCID = $row['C_ID'];
ShowTableHeader($previousCID);
do
{
// different C_ID than before ? Let's build a new <table>
if ($previousCID != $row['C_ID'])
{
echo "</table>";
$previousCID = $row['C_ID'];
ShowTableHeader($previousCID);
}
?>
<tr>
<td style="text-align:center"><?php echo $row['Item']; ?></td>
<td style="text-align:center"><?php echo $row['Personal']; ?></td>
<td style="text-align:center"><?php echo $row['Department']; ?></td>
<td style="text-align:center"><?php echo $row['Quantity']; ?></td>
<td style="text-align:center"><?php echo $row['Color']; ?></td>
</tr>
<?php
} while ($row=mysqli_fetch_assoc($sql));
echo "</table>";
}
}
Please help me, how to compare 2 name from different query?
I want to compare subject1 with subject2 if its same words it will printing 'YES' (like the image).
So this is my code, my code just read the last query from subject1.
So how can I compare all query from subject1 with all subject2?
<table class="table table-bordered">
<tr>
<td class="table-bordered">No</td>
<td class="table-bordered">Date</td>
<td class="table-bordered">Subject Name</td>
<td class="table-bordered">Subject No</td>
<td class="table-bordered">Revision No</td>
<td class="table-bordered">Trainer</td>
<td class="table-bordered">Institution</td>
</tr>
<?php
$query = mysql_query("SELECT * FROM training_detail AS s JOIN materi AS t JOIN training_history AS u JOIN employee AS v WHERE v.employee_id = $id1 AND s.nik LIKE v.nik AND u.id_materi LIKE t.id_materi AND s.id_riwayat_training LIKE u.id_riwayat_training ");
$i=1;
while($row1 = mysql_fetch_array($query))
{
$date = $row1['date'];
$subject1 = $row1['subject_name'];
?>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo date("j/F/Y", strtotime($date)); ?></td>
<td class="table-bordered"><?php echo $subject1; ?></td>
<td class="table-bordered"><?php echo $row1['subject_no']; ?></td>
<td class="table-bordered"><?php echo $row1['revision_no']; ?></td>
<td class="table-bordered"><?php echo $row1['trainer']; ?></td>
<td class="table-bordered"><?php echo $row1['institution']; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</br>
<table class="table table-bordered">
<tr>
Training yang belum diikuti
<td class="table-bordered">No</td>
<td class="table-bordered"></td>
<td class="table-bordered">subject id</td>
<td class="table-bordered">subject namei</td>
<td class="table-bordered">subject no</td>
</tr>
<?php
$query = mysql_query("SELECT * FROM job_name AS r JOIN subject AS q JOIN employee AS p WHERE q.subject_id LIKE r.header_id AND r.job_id LIKE p.id_jabatan AND p.id_karyawan = $id1 ORDER BY q.id_materi ASC ");
$i=1;
while($row2 = mysql_fetch_array($query))
{
$subject2 = $row2['subject_name'];
if (strcasecmp($subject1, $subject2) == 0)
{
?>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo "YES" ?></td>
<td class="table-bordered"><?php echo $row2['subject_id'] ?></td>
<td class="table-bordered"><?php echo $subject2; ?></td>
<td class="table-bordered"><?php echo $row2['subject_no']; ?></td>
</tr>
<?php
}
else
{
?>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo "NO" ?></td>
<td class="table-bordered"><?php echo $row2['subject_id'] ?></td>
<td class="table-bordered"><?php echo $subject2; ?></td>
<td class="table-bordered"><?php echo $row2['subject_no']; ?></td>
</tr>
<?php
}
$i++;
}
?>
</table>
You need to use nested loop I guess. Use second while inside of first one.
BTW look at http://php.net/manual/en/control-structures.alternative-syntax.php, it might make your code seem better.
When we run a query in ssms, we can get one or more rows of data per associateID.
However, on a table data,
<table>
<tr>
<td>...</td>
</tr>
</table>
We believe that the solution to our problem is to loop the data and dump contents in a table.
Any ideas how I can accomplish this?
Here is the code I am trying to loop:
<?php
//I need the loop here.
<table>
<tr>
<td class="dataItem" id="SignCode"></td>
<td class="dataItem" id="SignType"></td>
<td class="dataItem" id="SignSize"></td>
<td class="dataItem" id="SignColor"></td>
<td class="dataItem" id="Facing"></td>
<td class="dataItem" id="HorizClear"></td>
<td class="dataItem" id="VertClear"></td>
<td class="dataItem" id="Angle"></td>
<td class="dataItem" id="ReflCoat"></td>
<td class="dataItem" id="Condition"></td>
<td class="dataItem" id="Status"></td>
</tr>
</table>
?>
The data is a combination of query and the Javascript array. The query is
$tsql="select * from mytable where associateId='$aid'";
Then all the form ids are in Javascript like this example:
dojo.byId("SignType").innerHTML = obj["SignType"];
If you show us your php code we can give you a better answer but your code should be somthing like this:
<table>
<?php
// Your loop
while($row = /*fetchData()*/) {
?>
<tr>
<td class="dataItem" id="SignCode"><?php echo $row['SignCode']; ?></td>
<td class="dataItem" id="SignType"><?php echo $row['SignType']; ?></td>
<td class="dataItem" id="SignSize"><?php echo $row['SignSize']; ?></td>
<td class="dataItem" id="SignColor"><?php echo $row['SignColor']; ?></td>
<td class="dataItem" id="Facing"><?php echo $row['Facing']; ?></td>
<td class="dataItem" id="HorizClear"><?php echo $row['HorizClear']; ?></td>
<td class="dataItem" id="VertClear"><?php echo $row['VertClear']; ?></td>
<td class="dataItem" id="Angle"><?php echo $row['Angle']; ?></td>
<td class="dataItem" id="ReflCoat"><?php echo $row['ReflCoat']; ?></td>
<td class="dataItem" id="Condition"><?php echo $row['Condition']; ?></td>
<td class="dataItem" id="Status"><?php echo $row['Status']; ?></td>
</tr>
<?php } ?>
</table>
The code should look something like this:
(It is still unclear how you acquire the data from the DB.)
<?php
$resultSet = ... // <-- somehow aquire a result-set you can loop through
?>
<table>
<?php while ($row = <somehow_get_next_row_from_$resultSet_as_associative_array>) {?>
<tr>
<td class="dataItem" id="SignCode"><?php echo($row["signCode"]); ?></td>
<td class="dataItem" id="SignSize"><?php echo($row["signSize"]); ?></td>
<td class="dataItem" id="SignColor"><?php echo($row["signColor"]); ?></td>
...
<td class="dataItem" id="Status"><?php echo($row["status"]); ?></td>
</tr>
<?php } ?>
</table>
In case the value for id (e.g. "SignCode" etc) is the same as the name of the DB column-name AND the query returns the columns in the order you want the HTML-table columns to be, then the part between <tr> and </tr> could be further simplified as:
...
<tr>
<?php forEach ($row as $key => $value) {?>
<td class="dataItem" id="<?php echo($key); ?>"><?php echo($value); ?></td>
<?php } ?>
</tr>
...
I'm at lost. What I wanted to do is, I want my website to allow the users to select a certain row(records) in my database and then redirect them to another webpage that will show them the full information about that certain record.. I don't know how could I connect those two web pages together using dynamic table/text..
Below is a portion of my code: ( This is the first webpage: )
mysql_select_db($database_rfq_portal, $rfq_portal);
$query_rfqrecord = "SELECT tblrfq.`RFQ_ID`, tblrfq.`Company_Name`, tblrfq.Service,
tblrfq.`Kind_of_Request`, tblrfq.Status, tblrfq.`Date` FROM tblrfq";
$rfqrecord = mysql_query($query_rfqrecord, $rfq_portal) or die(mysql_error());
$row_rfqrecord = mysql_fetch_assoc($rfqrecord);
$totalRows_rfqrecord = mysql_num_rows($rfqrecord);
<form id="viewform" name="viewform" method="get" action="ViewSpecificRFQ.php">
<table width="716" border="1" align="center" cellpadding="5">
<tr>
<td>RFQ ID</td>
<td>Company Name</td>
<td>Service</td>
<td>Kind of Request</td>
<td>Status</td>
<td>Date</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_rfqrecord['RFQ_ID']; ?></td>
<td><?php echo $row_rfqrecord['Company_Name']; ?></td>
<td><?php echo $row_rfqrecord['Service']; ?></td>
<td><?php echo $row_rfqrecord['Kind_of_Request']; ?></td>
<td><?php echo $row_rfqrecord['Status']; ?></td>
<td><?php echo $row_rfqrecord['Date']; ?></td>
</tr>
<?php } while ($row_rfqrecord = mysql_fetch_assoc($rfqrecord)); ?>
</table>
}
</form>
This is the webpage that will get the form..(portion of my code)
$RFQID = $_GET['RFQ_ID'];
mysql_select_db($database_rfq_portal, $rfq_portal);
$query_rfqrecord = "SELECT * FROM tblrfq WHERE $RFQID";
$rfqrecord = mysql_query($query_rfqrecord, $rfq_portal) or die(mysql_error());
$row_rfqrecord = mysql_fetch_assoc($rfqrecord);
$totalRows_rfqrecord = mysql_num_rows($rfqrecord);
mysql_select_db($database_rfq_portal, $rfq_portal);
$query_user = "SELECT tbluser.Username, tbluser.Password FROM tbluser";
$user = mysql_query($query_user, $rfq_portal) or die(mysql_error());
$row_user = mysql_fetch_assoc($user);
$totalRows_user = mysql_num_rows($user);
<table width="716" border="0" align="center">
<tr>
<th colspan="2" scope="row">RFQ ID:</th>
<td><?php echo $row_rfqrecord['RFQ_ID']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Company Name:</th>
<td width="511"><?php echo $row_rfqrecord['Company_Name']; ?></td>
</tr>
<tr>
<th width="101" rowspan="2" scope="row">Address:</th>
<th width="90" scope="row">Site A:</th>
<td><?php echo $row_rfqrecord['Address_A']; ?></td>
</tr>
<tr>
<th scope="row">Site B:</th>
<td><?php echo $row_rfqrecord['Address_B']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Contact Number:</th>
<td><?php echo $row_rfqrecord['Contact_Number']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Contact Person:</th>
<td><?php echo $row_rfqrecord['Contact_Person']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Service:</th>
<td><?php echo $row_rfqrecord['Service']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Bandwidth:</th>
<td><?php echo $row_rfqrecord['Bandwidth']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Telco:</th>
<td><?php echo $row_rfqrecord['Telco']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Account Manager:</th>
<td><?php echo $row_rfqrecord['Account_Manager']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Status:</th>
<td><?php echo $row_rfqrecord['Status']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Kind of Request:</th>
<td><?php echo $row_rfqrecord['Kind_of_Request']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Date:</th>
<td><?php echo $row_rfqrecord['Date']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Remarks:</th>
<td><?php echo $row_rfqrecord['Remarks']; ?></td>
</tr>
</table>
</form>
this redirects the user to the next page but my problem is it keeps on showing the same record, which is the first record in my database.
1- Consider page1.php is the first page that user selects a record and then in page2.php you show full detail.
for page1.php
you need to send some parameters like record id or any other key in order to be able to identify the record and select the detail in database.
for example:
Record 1 once user clicked on this link you have to do something like this in page2.php, remember our parameter is record_id
<?php
$id = $_GET['record_id'];
//we have to check it for validity
//if id is an integer (numbers) then simply we can check it
//if(!is_numeric($id)) { die("invalid id") }
// now the id is there
//lets build query
$query = "SELECT * from tablename where id= '$id' ";
?>
for your code it must be like this:
//adding where clause
$query_rfqrecord = "SELECT * FROM tblrfq where RFQ_ID = '$id' ";
$rfqrecord = mysql_query($query_rfqrecord, $rfq_portal) or die(mysql_error());
$row_rfqrecord = mysql_fetch_assoc($rfqrecord);
$totalRows_rfqrecord = mysql_num_rows($rfqrecord);
If you want your users to get to load a specific record when they click a specific row, you have to be sure you're redirecting them to a specific url. In your sample you have <a href="ViewSpecificRFQ.php"> in your do-while, so every row in your table will have the same link and then the same destination page.
You probably want something like <a href="ViewSpecificRFQ.php?recordId=<?php echo $row_rfqrecord['RFQ_ID'] ?>"> and then in your landing page the query will be performed against the key you will have in $_GET['recordId'].
EDIT: The do-while problem
You use a do-while, and so in your first iteration you have no record loaded, because the fetch instruction is located at the bottom of the code block, so you should change it to:
<?php
while ($row_rfqrecord = mysql_fetch_assoc($rfqrecord)) {
?>
<tr>
<td><?php echo $row_rfqrecord['RFQ_ID']; ?></td>
<td><?php echo $row_rfqrecord['Company_Name']; ?></td>
<td><?php echo $row_rfqrecord['Service']; ?></td>
<td><?php echo $row_rfqrecord['Kind_of_Request']; ?></td>
<td><?php echo $row_rfqrecord['Status']; ?></td>
<td><?php echo $row_rfqrecord['Date']; ?></td>
</tr>
<?php
}
?>
I have a table containing several columns, one of which is a date column (data1).
The mysql query used is
SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()
We then take the data from each row, run some calculations and store this as a separate variables.
I would now like to compare this data with data from last week (in the same table), i.e. by changing the SELECT query.
Let me expand...
Query for getting this weeks data from table:
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()";
Now we run through extracting the data
$result = mysql_query($sqld) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$referred = $referred + $row['referred'];
$invalidated = $invalidated + $row['invalidated'];
$tobequalified = $tobequalified + $row['tobequalified'];
}
(the above is just a snippet of the calculations we need to run to demonstrate).
Now we display the results based on this weeks data
<h4>Totals for this week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
I'd like to now change the $sqld query above to select rows in the table that fall into last week, run the same calculations above and display the results below so we can compare the two.
<h4> Totals for last week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
Is there any way of achieving this without copying everything and changing the $sqld query?
I think you can create a function for repeting data and call it with some parameter to change the query like below
somefunction("Previous");
somefunction();
function somefunciton($query = "current") {
if($query == "Current")
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()";
else
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 2 WEEK) and (now(),INTERVAL 1 WEEK)";
$result = mysql_query($sqld) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$referred = $referred + $row['referred'];
$invalidated = $invalidated + $row['invalidated'];
$tobequalified = $tobequalified + $row['tobequalified'];
}
<h4>Totals for this week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
}
$result1 = mysql_query("SELECT * FROM leads");
$result2 = mysql_query("SELECT * FROM other table");
$arr1 = new Array();
$arr2 = new Array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$arr1['FirstName'] = $row['your column name'];
}
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$arr2['FirstName'] = $row['your column name'];
}
now you have your two arrays($arr1 and $arr2) and you compare as per you want...