Duplicated Results on SQL Query - php

PHP newbie here! I ve been struggling with this for a few days now and i have decided i cant figure this out on my own.
Basically i have 2 database tables "projects_2016" and "attachment".
I want to show the data of "projects_2016" to show in the top table and then check for a matching id number (and if it exsits) in "attachment" it will list all the results under the "project_2016 data".
At the moment it works great but it duplicates the "projects_2016" data for every "attachment" entry.
Here is my code, any input is appreciated!
PS not too concereded about Sql injections. Still learning that!
<?php include '../../../connection_config.php';
$sql = "SELECT DISTINCT * FROM attachment JOIN projects_2016 ON attachment.attachment_ABE_project_number = projects_2016.id ORDER BY `attachment_ABE_project_number` DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<table width="20" border="1" cellspacing="0" cellpadding="2">
<tr>
<th height="0" scope="col"><table width="990" border="0" align= "center" cellpadding="3" cellspacing="0">
<tr class="text_report">
<td width="107" height="30" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>PNo</strong></td>
<td width="871" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>Project Name</strong></td>
</tr>
<tr>
<td height="20" align="left" valign="middle" bgcolor="#FFFFFF" class="text_report"><strong><?php echo "<br>". $row["ID"]. "<br>";?></strong></td>
<td height="20" align="left" valign="middle" bgcolor= "#FFFFFF" class="text_report"><strong><?php echo "<br>". $row["project_name"]. "<br>";?></strong></td>
</tr>
</table>
<?php
$photo_id = $row["ID"];
$contacts = "SELECT DISTINCT * FROM attachment WHERE attachment_ABE_project_number = '$photo_id'" ;
$result_contacts = $conn->query($contacts);
if ($result_contacts->num_rows > 0) {
// output data of each row
while($row_contacts = $result_contacts->fetch_assoc()) {
?>
<table width="990" border="0" align="center" cellpadding= "3" cellspacing="0" class="text_report">
<tr>
<td height="0" colspan="4" align="left" valign="middle" nowrap="nowrap" bgcolor="#FFFFFF"> </td>
</tr>
<tr>
<td width="319" height="30" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>File Name</strong></td>
<td width="279" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>File Type</strong></td>
<td width="315" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>File Size (KB)</strong></td>
<td width="53" align="right" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>View File</strong></td>
</tr>
<tr>
<td height="20" align="left" valign="middle" bgcolor="#FFFFFF"><?php echo $row_contacts ['file'] ?></td>
<td height="20" align="left" valign="middle" bgcolor="#FFFFFF"><?php echo $row_contacts ['type'] ?></td>
<td height="20" align="left" valign="middle" bgcolor="#FFFFFF"><?php echo $row_contacts ['size'] ?></td>
<td align="right" valign="middle" bgcolor="#FFFFFF">view file</td>
</tr>
<tr>
<td height="0" colspan="4" align="left" valign="middle" bgcolor="#FFFFFF"> </td>
</tr>
<?php
}
?>
</table>
<?php
}
?></th>
</tr>
</table>
<table width="1000" border="0" cellspacing="0" cellpadding="0">
<tr>
<th height="26"> </th>
</tr>
</table>
<p>
<?php
}
}
?>
</p>
</table>
<?php $conn->close();
?>

$sql = "SELECT * FROM projects_2016
WHERE EXISTS (SELECT * FROM attachment WHERE projects_2016.id = attachment_ABE_project_number) ORDER BY id DESC ";

Related

Simple Dom Parser or CURL TABLE PARSING

I need help to get the data from a table. It's an internet usage table and the html code is down below :
<table width="572" border="0" align="center" cellspacing="0">
<tbody><tr valign="top">
<td width="1" class="bgsidelines"></td>
<td width="*" class="bgbottom">
<table summary="" width="100%" border="0" cellpadding="0">
<tbody><tr>
<td width="10" rowspan="2" bgcolor="#CCCCCC"></td>
<td width="443">
<table width="443" height="10" border="0" align="center" cellpadding="8">
<tbody>
<tr>
<td width="100%" class="path"><b>Internet usage</b></td>
</tr>
<tr>
<td class="reg"><!-- Begin yours codes -->
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody><tr>
<table cellpadding="5" cellspacing="1" border="0">
<tbody>
<tr>
<td width="43" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
<td width="60" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="60" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" class="reg" nowrap="nowrap">2017-06-01 to<br>2017-
06-18</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">54815.06</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">53.53</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">52114.59</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">50.89</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">106929.65</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">104.42</td>
</tr>
</tbody></table></td></tr>
</tbody></table>
<!-- End yours codes -->
</tr>
</tbody></table></td></tr>
</tbody></table></td></tr>
</tbody></table>
I've done it in a way that works but only works sometimes, this must be due to the user agent. and it fetches the entire table while I would like each separated values for the internet usage, the ones in the td class="reg" (54815.06, 53.53..) It's hard because there is a table in table.. Also it's
My PHP :
require_once 'advanced_html_dom.php';
$numvl = $_POST['numvl'];
$url =
'https://extranet.videotron.com/services/secur/extranet/tpia/Usage.do?
compteInternet='.$numvl;
$html = new AdvancedHtmlDom();
$html->load_file($url);
$element = $html->find("tr");
echo $element[1]->innertext;
no need for some external lib (advanced_html_dom.php? never heard of), just use PHP's DOMDocument and DOMXPath.
example:
<?php
declare(strict_types=1);
$domd=#DOMDocument::loadHTML(getHTML());
$xpath=new DOMXPath($domd);
foreach($xpath->query("//td[#valign='top' and #class='reg']") as $ele){
var_dump($ele->textContent);
}
function getHTML():string{
$html=<<<'HTML'
<table width="572" border="0" align="center" cellspacing="0">
<tbody><tr valign="top">
<td width="1" class="bgsidelines"></td>
<td width="*" class="bgbottom">
<table summary="" width="100%" border="0" cellpadding="0">
<tbody><tr>
<td width="10" rowspan="2" bgcolor="#CCCCCC"></td>
<td width="443">
<table width="443" height="10" border="0" align="center" cellpadding="8">
<tbody>
<tr>
<td width="100%" class="path"><b>Internet usage</b></td>
</tr>
<tr>
<td class="reg"><!-- Begin yours codes -->
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody><tr>
<table cellpadding="5" cellspacing="1" border="0">
<tbody>
<tr>
<td width="43" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
<td width="60" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="60" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" class="reg" nowrap="nowrap">2017-06-01 to<br>2017-
06-18</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">54815.06</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">53.53</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">52114.59</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">50.89</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">106929.65</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">104.42</td>
</tr>
</tbody></table></td></tr>
</tbody></table>
<!-- End yours codes -->
</tr>
</tbody></table></td></tr>
</tbody></table></td></tr>
</tbody></table>
HTML;
return $html;
}
output:
string(8) "54815.06"
string(5) "53.53"
string(8) "52114.59"
string(5) "50.89"
string(9) "106929.65"
string(6) "104.42"

Class not displaying correctly

This is the code I am using to display a row in a table:
$msg.='<tr class="'.$outclass.'" onClick="popticket('.$ticket['TicketID'].');" onmouseover="this.className='.$overclass.';" onmouseout="this.className='.$outclass.';" '.$reg1.' '.$reg2.' >
<td width="10%" align="center" class="style28" style="padding: 5px;">'.$ticket['TicketID'].'</td>
<td width="25%" align="left" class="style28">'.substr($ticket['customer_bus_name'], 0, 34).'</td>
<td width="12%" align="center" class="style28">'.$newpr.'</td>
<td width="15%" align="center" class="style28">'.$status.'</td>
<td class="style28">'.strip_tags($ticket['desc']).'</td>
<td width="60px" align="center" class="style28"><span style="display:none;">'.$ticket['updated'].'</span>'.$new_up.'</td>
<td width="45px" align="center" class="style28"><span style="display:none;">'.$ticket['scheduled'].'</span>'.$new_im.'</td>
</tr>';
Here each PHP variable has its own value. The problem is that the class given in the onmouseover and mouseout is not working.
I want to get this row as (this is the correct output):
<tr class="ticketrowsoutread" onmouseout="this.className='ticketrowsoutread';" onmouseover="this.className='ticketrowsover ticketrowsover2';" onclick="popticket('232');">
<td width="10%" align="center" class="style28" style="padding: 5px;">232</td>
<td width="25%" align="left" class="style28">net works</td>
<td width="12%" align="center" class="style28">Medium</td>
<td width="15%" align="center" class="style28">in progress</td>
<td class="style28">adsfasdf</td>
<td width="60px" align="center" class="style28"><span style="display:none;">1334709567</span>1 day</td>
<td width="45px" align="center" class="style28"><span style="display:none;">-</span></td>
</tr>
When I run my code it displays onmouseout="this.className=ticketrowsoutread;" instead of onmouseout="this.className='ticketrowsoutread';".
How can I get the desired output?
Add backslashes to escape the qote
onmouseover="this.className=\''.$overclass.'\';"

order by date mysql query

I used the following code to display the event details fetched from the database.
<form name="event_form" id="event_form" action="" method="post" enctype="application/x-www-form-urlencoded">
<table width="765" border="0" align="left" cellpadding="0" cellspacing="0" >
<tr>
<td>
<table width="765" border="0" align="left" cellpadding="0" cellspacing="0" id="results" class="fronttbl">
<tr></tr>
<?php
$select = "SELECT * FROM `tbl_event`";
$select_event = mysql_query($select);
$select_num = mysql_num_rows($select_event);
if($select_num > 0)
{
while($fetch = mysql_fetch_array($select_event))
{
$feventid=$fetch['intEventid'];
$feventname=stripslashes(ucfirst($fetch['varEventname']));
$fDate=$fetch['varDate'];
$seperate=explode("-", $fDate );
$year=$seperate[0];
$month=$seperate[1];
$date=$seperate[2];
$fchiefguest=stripslashes(nl2br($fetch['varChiefguest']));
$fvenue=stripslashes($fetch['varVenue']);
$ftime=stripslashes($fetch['varTime']);
$feventdetails=stripslashes($fetch['varEventdetails']);
echo " ";
?>
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="30%" height="30" valign="top"><strong>Name of the event:</strong></td>
<td width="70%" height="30" valign="top"><?php echo $feventname; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Date of the event to be held:</strong></td>
<td height="30" valign="top"><?php echo $date.'-'.$month.'-'.$year; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Time of the Event:</strong></td>
<td height="30" valign="top"><?php echo $ftime; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Venue of the event:</strong></td>
<td height="30" valign="top"><?php echo $fvenue; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Name of the Chief Guest:</strong></td>
<td height="30" valign="top"><?php echo $fchiefguest; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Event Details:</strong></td>
<td height="30" valign="top"><?php echo $feventdetails; ?></td>
</tr>
</table>
<p style="border-bottom:1px dotted #CCCCCC;"></p>
</td></tr>
<?php }
}
?>
</table>
</td>
</tr>
</table>
<div id="pageNavPosition"></div>
</form>
I have date field in my database table and the input will be saved like the following format 2012-03-01.
I need my page displays the events order by recent date/month/year in the frontend. For example today's event should displayed first likewise. How can i do that?
Use ORDER BY (...is that what you meant)?
Change your query to:
SELECT *
FROM `tbl_event`
ORDER BY varDate DESC
as i understand use this function for front end:
function changeFormat( $date ){
$exp_date = explode('-', $date);
return $exp_date[2] . '-'.$exp_date[1].'-'.$exp_date[0];
}
example: echo changeFormat('2012-12-01'); will return 01-12-2012 and apply this in your while loop where you want to display your date

Fetching the particular values from a table onclick of checkbox in php

My table has the following structure
When i click the particular checkbox.Its should be able to fetch the value of all the contents in the selected checkbox ..It can be n number of products the user selects.How to achieve this .... Thanks for ur time
Source code
<?$this->load->view('admin/header');?>
<script type="text/javascript">
function go() {
var frm = document.frm;
var status = inwardProductList['status'];
var statusList = [];
for ( i = 0; i < status.length; i++ ) {
if ( status[i].checked ) {
statusList.push('status=' + status[i].value);
}
}
document.getElementById('test').innerHTML = 'ajaxUrl?' + statusList.join('&');
}
</script>
<script src="<?=base_url();?>js/calendar.js" type="text/javascript"></script>
<form name="inwardProductList" action="" method="post" >
<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center" class="formtable">
<tr>
<td height="23" colspan="8" align="center" valign="middle" bgcolor="#FFFFFF" class="rows"><b>Cart Display</b></td>
</tr>
<tr>
<td height="66" align="left" valign="top"><table width="99%" id="suppliedtable" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="4%" height="43" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Sl.no</strong></td>
<td width="20%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Product Name</strong></strong></td>
<td width="20%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Barcode</strong></td>
<td width="8%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Selection</strong></strong></td>
<td width="8%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Quantity</strong></strong></td>
</tr>
<?
$i=0;
if($productName->num_rows() >0){
foreach($productName->result() as $row ){
$i++;
?>
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><?=$i;?></td>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><?=$row->product_name?></td>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><?=$row->barcode?></td>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><form>
<input type="checkbox" name="status" value="yes" /> Yes<br /></form></td>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><form>Quantity: <input type="text" name="Qunatity" /></form></td>
</tr>
<? }}else{?>
<tr>
<td height="23" colspan="8" align="center" valign="middle" bgcolor="#FFFFFF" class="rows"><b>Selected product has not been processed yet</b></td>
</tr>
<?}?>
</table></td>
</tr>
</table>
<form><tr><td align="center" > <button onclick="go()">go</button></td> </tr>
<div id="test"></div>
</form></form>
</form>
This could be achieved using jquery, something like this, though its off the top of my head so there may be errors.
Jquery stuff
$('tr.tableRow > input[type=checkbox]').click(function() {
var productName = $(this).find('td.productName').text();
var productBarcode = $(this).find('td.productBarcode').text();
var productQuantity = $(this).find('input[name=productQuantity]').val();
});
HTML stuff
<tr class="table-row">
<td class="productName">My Product</td>
<td class="productBarcode">0389463844</td>
<td class="productSelected"><input type="checkbox" value="yes"></input></td>
<td class="productQuantity"><input type="text" name="productQuantity"></td>
</tr>

Php HTML DOM parsing

<table width="100%" cellspacing="0" cellpadding="0" border="0" id="Table4">
<tbody>
<tr>
<td valign="top" class="tx-strong-dgrey">
<a class="anc-noul" href="http://www.example.com/catalog/proddetail.asp?logon=&langid=EN&sku_id=0665000FS10129471&catid=25653">
Apple 8GB 3rd Generation iPod Touch</a></td>
</tr>
<tr>
<td valign="top" class="element-spacer"/>
</tr>
<tr>
<td valign="top" class="tx-normal-grey">
Product detail
<a href="http://www.example.com/catalog/proddetail.asp?logon=&langid=EN&sku_id=0665000FS10129471&catid=25653">
More Info</a></td>
</tr>
<tr>
<td valign="top" class="element-spacer"/>
</tr>
<tr>
<td valign="top" class="tx-normal-red">
<span class="tx-strong-dgrey">Price:</span>
$189.99</td>
</tr>
<tr>
<td valign="top">You save: $9.00 after instant savings</td>
</tr>
<tr>
<td valign="top" class="element-spacer"/>
</tr>
<tr>
<td valign="top" class="tx-normal-grey">
<a href="http://www.example.com/catalog/subclass.asp?catid=25653&logon=&langid=EN">
View similar products</a>
<a href="http://www.example.com/catalog/mfr.asp?man=Apple&catid=19&logon=&langid=EN">
View similar products with same brand</a>
</td></tr>
<tr>
<td valign="top" class="element-spacer"/>
</tr>
</tbody>
</table>
I want to be able to get the $189.99.
echo $ret[0]->find('tr', 4)->plaintext;
This outputs: 'Price: $189.99'
I just need $189.99, not 'Price:'
$exp = explode(":", $ret[0]->find('tr', 4)->plaintext);
$price =$exp[1];

Categories