I use an html string in php to display sql query results in a single column:
How can I use multiple columns to lay it out like this:
while($row = mysqli_fetch_array($q)){
$id = $row['id'];
$title = $row['title'];
$desc = $row['description'];
$link = $row['link'];
$output .= '<a href="' . $link . '">
<h3>' . $title . '</h3>
<p>' . $desc . '</p>
</a>';
}
Credit to hungrykoala for help on this one
$i = 0 ;
$resultsPerRow = 2 ;
$output = '<tr>';
while($row = mysqli_fetch_array($q)){
$i++;
$id = $row['id'];
$title = $row['title'];
$desc = $row['description'];
$link = $row['link'];
$output .= '<td>' .$title. '<br>' .$desc. '</td>';
if ($i % $resultsPerRow == 0) {
$output .= '</tr><td><br></td><tr>';
}
}
echo($output);
Then output to a table tag
Related
OC 1.5.6
I've installed a module to filter products in categories based on manufacturer.
I'm able to reach every "search result page" at an url like:
example.com/index.php?route=product/category&path=X&manufacturer=Y
I'd like to create a SEO url like:
example.com/alias-for-category/alias-for-manufacturer
(ex. /bikes/honda or /cars/bmw)
but I can't figure out how to do it.
I've tried for testing and simplifying to convert just a single url:
product/category&path=1&manufacturer=1
so I added to url_alias a record with this query and keyword
cat1-man1
then I modified catalog/common/seo_url.php
function Index
if (isset($this->request->get['product_id'])) {
$this->request->get['route'] = 'product/product';
} elseif (isset($this->request->get['path'])) {
$this->request->get['route'] = 'product/category';
} elseif (isset($this->request->get['manufacturer_id'])) {
$this->request->get['route'] = 'product/manufacturer/info';
} elseif (isset($this->request->get['information_id'])) {
$this->request->get['route'] = 'information/information';
}
//ADDED BELOW CODE TO ROUTE ALL URL
else {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($this->request->get['_route_']) . "'");
if ($query->num_rows) {
$this->request->get['route'] = $query->row['query'];
}
}
and function rewrite
if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
unset($data[$key]);
}
} elseif ($key == 'path') {
$categories = explode('_', $value);
foreach ($categories as $category) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
}
}
unset($data[$key]);
}
//ADDED CODE BELOW TO CONVERT ALL URL
else {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($data['route']) . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
unset($data[$key]);
}
}
but also this simplified version doesn't work at all, page not found is shown.
What I'm doing wrong? Any tip will be really appreciated
First of all the custom module I've installed expects the parameter for manufacturer as "manufacturer" instead of "manufacturer_id" so a converted URL like :
example.com/index.php?route=product/category&path=X&manufacturer_id=Y
isn't handled correctly by the module.
So the answer to my question is : OC does already convert alias-cat/alias-manufacturer to a correct internal URL
On the other hand OC does not convert URL of the type
example.com/index.php?route=product/category&path=X&manufacturer_id=Y
into SEO friendly URL like:
example.com/alias-for-category/alias-for-manufacturer
in the menus or links, to do this, modify catalog/common/seo_url.php like this:
public function rewrite($link) {
$url_info = parse_url(str_replace('&', '&', $link));
$url = '';
$data = array();
parse_str($url_info['query'], $data);
foreach ($data as $key => $value) {
if (isset($data['route'])) {
if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
unset($data[$key]);
}
} elseif ($key == 'path') {
$categories = explode('_', $value);
foreach ($categories as $category) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
}
}
unset($data[$key]);
}
// ADD THIS CODE
elseif ($key == 'manufacturer_id') {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'manufacturer_id=" . (int)$value . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
}
unset($data[$key]);
}
// END ADDED CODE----------------
else {
.....
I have this kind of result, absent to Current wolcha was ok but i want to get the previous status of wolcha by getting or displaying only the total previous wolcha no need for the absent, lates etc. just the total.
I have this kind of result, absent to Current wolcha was ok but i want to get the previous status of wolcha by getting or displaying only the total previous wolcha no need for the absent, lates etc. just the total.
function getwolchastatus($salon_id){
global $db;
$query = "SELECT employee_attendance.emp_id,
employee_attendance.date,
employee_attendance.branch_id,
employee_attendance.user_id,
employee_profile.emp_no,
COUNT(IF(employee_attendance.status='A',1, NULL)) 'absent',
COUNT(IF(employee_attendance.status='L',1, NULL)) 'late',
COUNT(IF(employee_attendance.status='SL',1, NULL)) 'sick_leave',
COUNT(IF(employee_attendance.status='LWP',1, NULL)) 'leave_w_pay',
COUNT(IF(employee_attendance.status='LWOP',1, NULL)) 'leave_wo_pay',
COUNT(IF(employee_attendance.status='HD',1, NULL)) 'halfday'
FROM employee_attendance
INNER JOIN employee_profile ON employee_profile.emp_id = employee_attendance.emp_id
INNER JOIN users ON users.user_id = employee_profile.user_id WHERE MONTH(employee_attendance.date) = MONTH(CURRENT_DATE) AND YEAR(employee_attendance.date) = YEAR(CURRENT_DATE) AND users.status = 'activate' AND users.salon_id = 2 GROUP BY employee_attendance.emp_id";
$result = $db->query($query) or die($db->error);
$content = '';
while($row = $result->fetch_array()) {
extract($row);
setlocale(LC_MONETARY, 'en_PH');
$query2 = "SELECT employee_attendance.emp_id,
employee_attendance.date,
employee_attendance.branch_id,
employee_attendance.user_id,
employee_profile.emp_no,
COUNT(IF(employee_attendance.status='A',1, NULL)) 'absent2',
COUNT(IF(employee_attendance.status='L',1, NULL)) 'late2',
COUNT(IF(employee_attendance.status='SL',1, NULL)) 'sick_leave2',
COUNT(IF(employee_attendance.status='LWP',1, NULL)) 'leave_w_pay2',
COUNT(IF(employee_attendance.status='LWOP',1, NULL)) 'leave_wo_pay2',
COUNT(IF(employee_attendance.status='HD',1, NULL)) 'halfday2'
FROM employee_attendance
INNER JOIN employee_profile ON employee_profile.emp_id = employee_attendance.emp_id
INNER JOIN users ON users.user_id = employee_profile.user_id WHERE employee_attendance.date between '2017-12-01' and '2017-12-31' AND users.status = 'activate' AND users.salon_id = 2 GROUP BY employee_attendance.emp_id";
$result2 = $db->query($query2) or die($db->error);
$row2 = $result2->fetch_array();
$new_branch = new Branch;
$employee = new Employee;
$new_user = new Users;
$branch_name = $new_branch->get_branch_info($row['branch_id'], 'branch_name');
$first_name = $employee->get_employee_info($row['emp_id'], 'first_name');
$last_name = $employee->get_employee_info($row['emp_id'], 'last_name');
$nickname = $employee->get_employee_info($row['emp_id'], 'nickname');
$employee_name = $nickname;
//$today = date('MONTH');
// $date1 = DATEADD('DAY', -30, GETDATE());
if(($row['absent'] <3) && ($row['late'] < 2) &&($row['sick_leave'] < 1) && ($row['leave_w_pay'] < 1) && ($row['leave_wo_pay'] < 1) && ($row['halfday'] <2)){
$row['total'] = 1;
}else{
$row['total'] = 0;
}
if(($row2['absent2'] <3) && ($row2['late2'] < 2) &&($row2['sick_leave2'] < 1) && ($row2['leave_w_pay2'] < 1) && ($row2['leave_wo_pay2'] < 1) && ($row2['halfday2'] <2)){
$row2['total2'] = 1;
}else{
$row2['total2'] = 0;
}
$total3 = $row['total'] + $row2['total2'];
$content .= "<tr>";
// $content .= "<td>";
// $content .= $branch_name;
// $content .= "</td>";
$content .= "<td>";
$content .= $last_name.' '.$first_name;
$content .= "</td>";
$content .= "<td>";
$content .= $row['absent'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['late'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['sick_leave'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['leave_w_pay'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['leave_wo_pay'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['halfday'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['total'];
$content .= "</td>";
$content .= "<td>";
$content .= $row2['total2'];
$content .= "</td>";
$content .= "<td>";
$content .= $total3;
$content .= "</td>";
$content .= "</tr>";
}
echo $content;
}
I have a good result using PDO query retrieve all data from database.
But, this only display the result with LIMIT like 10.
My questions now is how to paste this result for paginate?
I want to set result for 10 perpage and have prev 1 2 3 4 next for all data.
below script is good and fast result for me.
<?php
//load database connection
$host = "localhost";
$user = "root";
$password = "";
$database_name = "";
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
// Search from MySQL database table
$search=$_POST['search'];
$query = $pdo->prepare("select * from table where colum1 LIKE '%$search%' OR colum2 LIKE '%$search%' LIMIT 0 , 10");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
if (!$query->rowCount() == 0) {
echo "Search found :<br/>";
echo "<table style=\"font-family:arial;color:#333333;\">";
echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Title Books</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Author</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Price</td></tr>";
while ($results = $query->fetch()) {
echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";
echo $results['name'];
echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";
echo $results['description'];
echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";
echo "$".$results['thumb'];
echo "</td></tr>";
}
echo "</table>";
} else {
echo 'Nothing found';
}
?>
I would suggest that you create a class for pagination then include the class.
pagination.php
<?php
class paginate
{
private $pdo;
function __construct($pdo)
{
$this->db = $pdo;
}
public function dataview($query,$Search)
{
$stmt = $this->db->prepare($query);
$stmt->execute(arary($Search,$Search));
$results = $stmt->fetchall();
if (count($results) > 0) {?>
Search found : <br/>
<table style="font-family:arial;color:#333333;">";
<tr><td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;">Title Books</td><td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;">Author</td><td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Price</td></tr>
<?php
foreach ($results as $row):
?>
<tr><td style="border-style:solid;border-width:1px;border-color:#98bf21;">
<?php echo $row['name'];?></td>
<td style="border-style:solid;border-width:1px;border-color:#98bf21;">;
<?php echo $row['description'];?>
</td>
<td style="border-style:solid;border-width:1px;border-color:#98bf21;">
<?php echo $row['thumb'];?>
</td></tr>
<?php
endforeach;
echo "</table>";
} else {
echo "<p> Nothing found </p>";
}
}
public function paging($query, $records_per_page)
{
$starting_position = 0;
if (isset($_GET['page_no'])) {
$starting_position = ($_GET["page_no"] - 1) * $records_per_page;
}
$query2 = $query . " limit $starting_position,$records_per_page";
return $query2;
}
public function paginglink($query, $records_per_page)
{
$self = $_SERVER['PHP_SELF'];
$stmt = $this->db->prepare($query);
$stmt->execute();
$total_no_of_records = $stmt->rowCount();
if ($total_no_of_records > 0) {
?>
<ul class="pagination"><?php
$total_no_of_pages = ceil($total_no_of_records / $records_per_page);
$current_page = 1;
if (isset($_GET["page_no"])) {
$current_page = $_GET["page_no"];
}
if ($current_page != 1) {
$previous = $current_page - 1;
echo "<li><a href='" . $self . "?page_no=1' >First</a></li>";
echo "<li><a href='" . $self . "?page_no=" . $previous . "'>Previous</a></li>";
}
for ($i = 1; $i <= $total_no_of_pages; $i++) {
if ($i == $current_page) {
echo "<li class='active'><a href='" . $self . "?page_no=" . $i . "' >" . $i . "</a></li>";
} else {
echo "<li><a href='" . $self . "?page_no=" . $i . "'>" . $i . "</a></li>";
}
}
if ($current_page != $total_no_of_pages) {
$next = $current_page + 1;
echo "<li><a href='" . $self . "?page_no=" . $next . "'>Next</a></li>";
echo "<li><a href='" . $self . "?page_no=" . $total_no_of_pages . "'>Last</a></li>";
}
?></ul>
<?php
}
}
}
?>
page.php
<?php
//your connection
$host = "localhost";
$user = "root";
$password = "";
$database_name = "";
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
include_once'pagination.php';
$paginate = new paginate($pdo);
$search=$_POST['search'];
$Search = "%$search%";
$query = "SELECT * from table where colum1 LIKE ? OR colum2 LIKE ? ";
$records_per_page=10;
$newquery = $paginate->paging($query,$records_per_page,$Search);
$paginate->dataview($newquery,$Search);
$paginate->paginglink($query,$records_per_page);
?>
This should work. Suggestions/comments are welcome.
This is one of the projects I have used the above code to do a pagination :
or use jquery dataTables which will also work perfect
all u need is to download https://datatables.net/
With Datatables, simple download it then add required scripts and css, then on your table add an ID, then initialize datatable
$('#TableID').dataTable();
You might have to do the proper table markup,
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr>
<td>Text</td>
</tr>
</tbody>
I want to skip the foreach loop;
How can I get the sum by using a yii query?
$sql = 'select size_kb from comp_arch_stats where company_id = ' . ($model->company_id) . ' and arch_month = ' . $month . ' and arch_year = ' . $year . ';';
$val = Yii::app()->db->createCommand($sql)->queryAll();
$sum = 0;
foreach ($val AS $result) {
$sum += $result['size_kb'];
}
"'select sum(size_kb) as size_kb from comp_arch_stats where company_id = ' . ($model->company_id)"
Try this
$criteria=new CDbCriteria;
$criteria->select = 'sum(size_kb) AS KbCount';
$criteria->condition ="company_id = :company_id AND arch_month =:arch_month AND arch_year=:arch_year "
$criteria->params = array (
':company_id '=> $model->company_id,
':arch_month' => $month,
':arch_year' => $year,
);
comp_arch_stats::model()->findAll($criteria);
PHP Query:
<?php
$query = $db->prepare('SELECT * FROM Locations WHERE user_id = :id LIMIT 0, 5');
$query->bindParam(":id",$id);
$result = $query->execute();
$rows = $query->fetch();
foreach ($rows as $row) {
echo $row["timestamp"]; "<br />";
}
?>
The two rows that should be printed (timestamp):
What actually prints:
1188((22
The error within the console:
PHP Warning: Illegal string offset 'timestamp' in /Sites/pages/user_account.php on line 73 - Line 73 being the echo $row... inside the forloop.
Any help would be greatly appreciated.
You are using fetch, which retrieves a single row, instead of fetchAll:
$rows = $query->fetchAll();
You have two rows (user_id = 8)
$rows = $query->fetchAll();
For all rows
foreach ($rows as $row) {
echo $row . "<br />";
}
For 1 row , all column
while ($row = $rows) {
foreach ($row as $column) {
echo $column . "<br />";
}
}