SQL Join in CodeIgniter - sql

I have one file, then for every file there are multiple comments about that file. I was able to successfully join the two tables, comments and files, however when I try to view the page where it displays the file and its comments, the page displays the name of file n times (n corresponds to the number of records of comments of that certain file).
How will I be able to display just once the file name as well as displaying the comments?
<table width="40%" align="center">
<?php foreach($rows as $file):?>
<tr>
<td width="70%" id="title"><?php echo $file->name; ?></td>
</tr>
<?php endforeach;?>
</table>
<table width="40%" align="center" frame="above" style="margin-top:10px; border-top-width:3px; border-top-color:#666666">
<?php foreach($rows as $file):?>
<tr>
<td width="15%"></td>
<td width="45%" id="name">FILESIZE:</td>
<td width="40%" id="txt3"><?php echo $file->size; ?></td>
</tr>
<tr>
<td></td>
<td id="name">DATE UPLOADED:</td>
<td id="txt3"><?php echo $file->dateUploaded;?></td>
</tr>
<?php endforeach;?>
</table>
<!--COMMENTS -->
<table align="center" frame="above" style="margin-top:10px; border-top-width:3px; border-top-color:#666666" width="40%">
<tr>
<td><h3>Comments</h3></td>
</tr>
<tr><?php foreach($rows as $comment):?>
<td><?php echo $comment->comm;?></td>
</tr><?php endforeach;?>
</table>

You can use
GROUP_CONCAT()
in your join.
SELECT person.id AS id, name, GROUP_CONCAT(role.role SEPARATOR ',') AS roles
FROM person
LEFT OUTER JOIN person_role ON person.id=person_role.person_id
LEFT OUTER JOIN role ON person_role.role_id=role.id
GROUP BY id
ORDER BY id;
is an example from a tutorial at
http://dev-loki.blogspot.com/2008/01/aggregate-join-results-with-mysql-using.html
can't give much better without seeing your schema.
EDIT: trying anyway.
SELECT files.*, GROUP_CONCAT(comments.comment SEPARATOR ',') as comments_list
FROM files
LEFT JOIN comments on files.id = comments.id
GROUP BY id
ORDER BY id

Adding onto orbit, in codeigniter if your using activerecord it would be:
(Sorry, I couldn't format code in a comment)
$this->db->select('person.id AS id, name, GROUP_CONCAT(role.role SEPARATOR ',') AS roles',false);
$this->db->from('person');
$this->db->join('person_role', 'person.id = person_role.person_id', 'left outer');
$this->db->join('role', 'person_role.role_id = role.id', 'left outer');
$this->db->group_by('id');
$this->db->order_by('id'):
$this->db->get();

well it looks like you're echoing the filename for each row that is returned
your rows are getting returned like
name | size | comment1
name | size | comment2
so just don't do foreach ($rows as $file) that will print out the filename each time, just change
<?php foreach($rows as $file):?>
<tr>
<td width="70%" id="title"><?php echo $file->name; ?></td>
</tr>
<?php endforeach;?>
to
<tr>
<td width="70%" id="title"><?php echo $rows[0]->name; ?></td>
</tr>

Related

Am having difficulty displaying data in a table using SQL Aggregate Function SUM()

I tried to use the SQL Aggregate Function SUM () and it successfully but i can't seem to be able to display this in a table
Below is my php code i tried it with
`$sql = " SELECT product_name, sum(unit_price_before_discount)
FROM transaction_sell_lines
GROUP BY product_name";
<!-- TABLE CONSTRUCTION -->
<table>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Current Value</th>
</tr>
<!-- PHP CODE TO FETCH DATA FROM ROWS -->
<?php
// LOOP TILL END OF DATA
while($rows=$result->fetch_assoc())
{
?>
<tr>
<!-- FETCHING DATA FROM EACH
ROW OF EVERY COLUMN -->
<td><?php echo $rows['product_name'];?></td>
<td><?php echo $rows['quantity'];?></td>
<td><?php echo $rows['unit_price_before_discount'];?></td>
<td><?php echo ($rows['unit_price_before_discount'])*$rows['quantity'];?></td>
</tr>
<?php
}
?>
</table>`
This is the result i get
And this is my database structure
What i want to do is to add all the values in quantity that has the same name together and display in a table

How to display user_nicename and usermeta values by custom query in Wordpress?

I am trying to display user_nicename and one of usermeta values where meta_key is description. My query is given below:
<?php
$querystr = "
SELECT *
FROM $wpdb->users, $wpdb->usermeta
WHERE $wpdb->users.ID = $wpdb->usermeta.user_id
AND $wpdb->usrmeta.meta_key = 'description'
";
$retrieve_data = $wpdb->get_results($querystr, OBJECT);
?>
<?php global $retrieved_data; foreach ($retrieve_data as $retrieved_data){ ?>
<table>
<tr>
<th>Customer Name</th>
<th>Customer Details</th>
</tr>
<tr>
<td><?php echo $retrieved_data->user_nicename;?></td>
<td><?php echo $retrieved_data->meta_value;?></td>
</tr>
</table>
<?php } ?>
My final output should be displayed inside a table which will look something like below:
But I am in confusion how to do that. I am facing problem for both sql syntax and displaying data inside the table. Sorry for this easy question, I am still a learner. Can anyone help me in here? Thanks

pdo count not working

I have a page and all the function is working my only problem now is ciunying the record from the databse..
Class.user.php
Public function data($count)
{
$stmt=$this->db->prepare("SELECT COUNT(*) FROM login");
$result=$this->db->prepare($count);
$result->execute();
$number_of_rows=$result->fetchColumn();
}
Index.php
<table>
<thead>
<tr>
<th>2014</th>
</tr>
</thead>
<tbody>
<?php
$count="SELECT COUNT(*) FROM login";
$crud->data($count);
?>
The problem is that its not showing the count..
You're writing the same query as function argument, but also inside the function itself. And you're only really using one. This is nonsense. Dedicate your method to return the count, don't make the query a parameter.
prepareing the statement is pointless in this case, since you're neither reusing it nor are you binding any values. You can simply query() it directly.
The clincher: you're neither outputting nor returning the count, so it's very very expected that it doesn't show up anywhere.
Here's a sane version:
public function getCount() {
$result = $this->db->query('SELECT COUNT(*) FROM login');
return $result->fetchColumn();
}
<tbody>
<tr>
<td>
<?php echo $crud->getCount(); ?>
I think I usually do it like this link
$sql= "SELECT COUNT(*) FROM login";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row =$stmt->fetchObject();
then to output it you would have to ECHO
<table>
<tbody>
<tr>
<td>
<?php echo $row['count'];?>
</td>
</tr>
</tbody>
</table>

identify the content of xml using header value in sql

DECLARE #xml xml=' <?xml version="1.0" encoding="UTF-8"?><text>
<table>
<thead>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>d</th>
<th>e</th>
<th>f</th>
</tr>
</thead>
<tbody>
<tr>
<td>testa1</td>
<td>testb1</td>
<td>testc1</td>
<td>testd1</td>
<td>teste1</td>
<td>testf1</td>
</tr>
<tr>
<td>testa1</td>
<td>testb1</td>
<td>testc1</td>
<td>testd1</td>
<td>teste1</td>
<td>testf1</td>
</tr>
</tbody>
</table>
</text>'
SELECT
T.c.value('(tbody/tr/td)[1]','VARCHAR(100)') AS a
FROM
#xml.nodes('text/table[thead[tr[th="b"]]]') AS t(c)
This query selects only the first body contents from xml.
Here I want to select only the values under header b
Modifying the xpath as follows will do it:
SELECT T.c.value('/text/table/tbody/tr/td[position()=2]','VARCHAR(100)') AS a
Explanation:
Selects the second td element among a list of siblings.
Note:
Combine that with this SO answer to get:
SELECT T.c.value('/text/table/tbody/tr/td[position() = count(/text/table/tbody/tr/th[text() = "b"]/preceding-sibling::*)+1]','VARCHAR(100)') AS a

order by in joined tables

i want to order the second joined table according to price, to exaplain it more clear i will add a screenshot: http://s42.radikal.ru/i098/1108/87/66f19d915bbc.jpg the second table displays the prices: bayi 1, bayi 2, bayi 3, bayi 4 and liste fiyati, i want to rearrange em in another order starting from liste fiyati and ending on bayi 1. here is my query:
<cfquery name="get_products" datasource="#dsn3#">
SELECT P.PRODUCT_ID,P.PRODUCT_NAME,PS.MONEY,PR.PRICE,P.BRAND_ID,PS.PRICE,GSL.PRODUCT_STOCK,GSL.PURCHASE_ORDER_STOCK
FROM PRODUCT P
JOIN PRICE_STANDART PS ON P.PRODUCT_ID = PS.PRODUCT_ID
JOIN PRICE PR ON P.PRODUCT_ID = PR.PRODUCT_ID
JOIN #DSN2_ALIAS#.GET_STOCK_LAST GSL ON P.PRODUCT_ID = GSL.PRODUCT_ID
WHERE P.IS_SALES=1 AND P.IS_INTERNET=1 AND PS.PURCHASESALES=1 AND PS.PRICESTANDART_STATUS=1
AND PR.STARTDATE <= #now()# AND (PR.FINISHDATE >= #now()# OR PR.FINISHDATE IS NULL)
GROUP BY P.PRODUCT_ID,PR.PRICE,P.PRODUCT_NAME,PS.MONEY,P.BRAND_ID,PS.PRICE,GSL.PRODUCT_STOCK,GSL.PURCHASE_ORDER_STOCK
ORDER BY PS.PRICE DESC
</cfquery>
and the table:
<table cellpadding="3" cellspacing="1" class="color-border" width="100%">
<tr class="color-header">
<td><b>Ürün</b></td>
<td class="header_bold" width="80"><b>Marka</b></td>
<td class="header_bold" width="35"><b>Stok</b></td>
<td class="header_bold" width="35"><b>Yoldaki Stok</b></td>
<td class="header_bold" width="80">Bayı 4</td>
<td class="header_bold" width="80">Bayı 3</td>
<td class="header_bold" width="80">Bayı 2</td>
<td class="header_bold" width="80">Bayı 1</td>
<td class="header_bold" width="80">Liste fiyatı</td>
<td class="header_bold" width="25">Para</td>
</tr>
<cfoutput query="get_products" startrow="#attributes.startrow#" maxrows="#attributes.maxrows#" group="product_id">
<tr height="20" onMouseOver="this.className='color-light';" onMouseOut="this.className='color-row';" class="color-row">
<td>#product_name#</td>
<td align="center"><cfif len(brand_list)>#get_brands.brand_name[listfind(brand_list,brand_id,',')]#</cfif></td>
<td align="center">#PRODUCT_STOCK#</td>
<td align="center">#purchase_order_stock#</td>
<cfoutput><td align="center">#tlformat(price,2)#</td></cfoutput>
<td align="center">#MONEY#</td>
</tr>
<cfset toplam_stock = toplam_stock+product_stock>
<cfset toplam_order_stock = toplam_order_stock+purchase_order_stock>
</cfoutput>
<tr class="color-header">
<td colspan="2"></td>
<td align="center"><cfoutput>#toplam_stock#</cfoutput></td>
<td align="center"><cfoutput>#toplam_order_stock#</cfoutput></td>
<td colspan="12"></td>
</tr>
</table>
btw, the price i want to order is PRICE PR not the PRICE_STANDART PS, and thank you all for help!
You just need to add both ORDER BY statements:
ORDER BY P.PRODUCT_ID, PS.PRICE DESC
With ColdFusion grouping, make sure that you always sort by what you want to group by first, then you can sort by whatever you need inside the group.
But then change the
ORDER BY PS.PRICE DESC
to
ORDER BY PR.PRICE DESC
Surely?
If I understand what you're asking, you need to include all the cols from the GROUP BY in your ORDER BY statement first, and then add any other columns that you want to order by within those groupings.
But I'm afraid to say yor question isn't terribly clear.
--
Adam