Convert PDO::FETCH_ASSOC to SQLite - php

I am hoping to convert some of my basic settings like my navigation, mysql settings (host, username, password) to SQLite but it seems from something I read this morning there is no equivalent for the command PDO::FETCH_ASSOC. Is there another way for me to do this query that would be compatible with SQLite?
<?
$sql = "SELECT * FROM menu_items WHERE status = 'ACTIVE' ORDER BY menu_parent_id ASC, sortorder ASC, menu_item_name ASC";
$query = $db->query($sql);
$menu_items = array();
while($data = $query->fetch(PDO::FETCH_ASSOC)) {
if($data['menu_parent_id'] == 0) {
$menu_items[$data['menu_item_id']] = array();
$menu_items[$data['menu_item_id']]['menu_item_id'] = $data['menu_item_id'];
$menu_items[$data['menu_item_id']]['name'] = $data['menu_item_name'];
$menu_items[$data['menu_item_id']]['url'] = $data['menu_url'];
$menu_items[$data['menu_item_id']]['fontawesome'] = $data['fontawesome'];
$menu_items[$data['menu_item_id']]['children'] = array();
} else if($data['menu_parent_id'] != 0) {
$tmp = array();
$tmp['menu_item_id'] = $data['menu_item_id'];
$tmp['name'] = $data['menu_item_name'];
$tmp['url'] = $data['menu_url'];
$tmp['fontawesome'] = $data['fontawesome'];
array_push($menu_items[$data['menu_parent_id']]['children'],$tmp);
unset($tmp);
}
}
function create_list($arr)
{
$html = "";
foreach($arr as $key => $value) {
//Here the menu item has children
if(count($value['children']) > 0) {
$html .= '<!-- PARENT --> <li class="mm-dropdown"> <i class="menu-icon fa '. $value['fontawesome']. '"></i><span class="mm-text">'.$value['name'].'</span>
<ul>';
// Here it is the child
foreach($value['children'] AS $child) {
$html .= '
<!-- child --><li><i class="menu-icon fa '. $child['fontawesome']. '"></i>'.$child['name'].'</li>';
}
$html .= ' </ul>
';
} else{
$html .= ' <a class="menu-icon fa '.$value['fontawesome'].'" id="'.$value['menu_item_id'].'" >'.$value['name'].'</a>';
}
}
return $html;
}
?>

So your $query is SQLite3Result ??
try this way then:
while($data = $query->fetchArray(SQLITE3_ASSOC)) {

Related

PHP file breaks when using $_GET variable in URL

I am using a select dropdown menu to filter clinical trials from a database. Below the select is code that calls clinical_trial() class. Problem is that no results are being displayed when $_GET variable ?cid= is appended to the url.
clinical_trials.php
<?php if($_GET['cid']) $cid = $_GET['cid']; ?>
<?php
$query = "SELECT * FROM `category` ORDER BY category_name";
$categories = $_db->get_results($query, ARRAY_A); ?>
<select id="dynamic_select">
<option value="clinical_trials.php" selected>All Categories</option>
<?php
foreach($categories as $row):
extract($row);
echo '<option ' . ($cid == $category_id ? "selected" : "") . ' value="clinical_trials.php?cid='.$category_id.'">' . $category_name . '</option>';
endforeach; ?>
</select>
<script>
jQuery(document).ready(function($){
$('#dynamic_select').on('change', function () {
var url = $(this).val();
if (url) window.location = url; // redirect
return false;
});
});
</script>
<?php
$ct = new clinical_trial();
$params = array();
if($cid != '') $params['category_id'] = $cid;
$results = $ct->search($params);
$file_path = CLINICAL_TRIALS_REL_PATH;
$ts = strtotime($file_date);
if(count($results) > 0):
$html = '';
$html .= '<table id="current-clinicals">';
foreach($results as $row):
extract($row);
$html .= '<tr>';
$html .= '<td valign="top">'.$trial_name.'</td>';
$html .= '<td valign="top">'.$category_name.'</td>';
$html .= '<td valign="top">'.date("m/d/Y").'</td>';
$html .= '<td width="80" valign="top" align="center"><strong>View Here</strong></td>';
$html .= '</tr>';
endforeach;
else:
$html .= '<p>No clinical trials in this category.</p>';
endif;
$html .= '</table>';
echo $html; ?>
Below code is stored in clinical_trial.php
<?php
class clinical_trial{
public function validate($post, $file, &$errors, $action='create'){
global $_db;
cleanup_arr($post);
extract($post);
$errors = array();
$rules[] = array( 'rule'=>'', 'val'=>$lst_category_id, 'minlen'=>0, 'maxlen'=>0, 'required'=>true, 'friendly_name'=>'Category', 'var'=>'lst_category_id');
$rules[] = array( 'rule'=>'', 'val'=>$txt_trial_name, 'minlen'=>0, 'maxlen'=>0, 'required'=>true, 'friendly_name'=>'Trial name', 'var'=>'txt_trial_name');
$rules[] = array( 'rule'=>'', 'val'=>$txt_file_date, 'minlen'=>0, 'maxlen'=>0, 'required'=>true, 'friendly_name'=>'File date', 'var'=>'txt_file_date');
$flag_validated = true;
foreach($rules as $r){
$ret = validate($r);
$varname = $r['var'];
if($ret != VALIDATE_SUCCESS){
$flag_validated = false;
$errors[$varname] = $ret;
}
}
if ($action == 'create'){
if(!is_uploaded_file($file['file_filename']['tmp_name'])){
$flag_validated = false;
$errors['file_filename'] = 'Please upload a file.';
}
}
return $flag_validated;
}
function create($post, $file){
global $_db;
cleanup_arr($post);
extract($post);
$ts = strtotime($txt_file_date);
$file_date = date("Y-m-d", $ts);
$query = "INSERT INTO `clinical_trial` (trial_name, file_date, file_name, category_id) VALUES ('$txt_trial_name', '$file_date', '', $lst_category_id)";
$_db->query($query);
$clinical_trial_id = $_db->insert_id;
//$filename = md5(time());
$filename = $file_date . '-' . make_file_name($txt_trial_name);
$filename = handle_file_upload($file['file_filename'], $filename, CLINICAL_TRIALS_ABS_PATH);
$_db->query("UPDATE `clinical_trial` SET file_name='$filename' WHERE clinical_trial_id=$clinical_trial_id");
}
function update($post, $file){
global $_db;
cleanup_arr($post);
extract($post);
$ts = strtotime($txt_file_date);
$file_date = date("Y-m-d", $ts);
$query = "UPDATE `clinical_trial` SET trial_name='$txt_trial_name', category_id=$lst_category_id, file_date='$file_date' WHERE clinical_trial_id=$hdn_clinical_trial_id";
$_db->query($query);
if(is_uploaded_file($file['file_filename']['tmp_name'])){
#unlink(CLINICAL_TRIALS_ABS_PATH . $_db->get_var("SELECT file_name FROM clinical_trial WHERE clinical_trial_id=$hdn_clinical_trial_id"));
$filename = $file_date . '-' . make_file_name($txt_trial_name);
$filename = handle_file_upload($file['file_filename'], $filename, CLINICAL_TRIALS_ABS_PATH);
$_db->query("UPDATE `clinical_trial` SET file_name='$filename' WHERE clinical_trial_id=$hdn_clinical_trial_id");
}
}
function delete($clinical_trial_id){
global $_db;
cleanup_var($clinical_trial_id);
#unlink(CLINICAL_TRIALS_ABS_PATH . $_db->get_var("SELECT file_name FROM clinical_trial WHERE clinical_trial_id=$clinical_trial_id"));
$_db->query("DELETE FROM `clinical_trial` WHERE clinical_trial_id=$clinical_trial_id");
}
function search($params, $order_by=''){
global $_db;
if($params){
cleanup_arr($params);
extract($params);
}
if($category_id != '') $where = " AND ct.category_id=$category_id ";
$order_by = $order_by == "" ? "file_date DESC" : $order_by;
$query = "SELECT * FROM `clinical_trial` ct, `category` c
WHERE ct.category_id=c.category_id
$where
ORDER BY $order_by";
return $_db->get_results($query, ARRAY_A);
}
public function get($id)
{
global $_db;
cleanup_var($id);
$query = "SELECT * FROM `clinical_trial` ct WHERE ct.clinical_trial_id=$id";
$r = $_db->get_row($query, ARRAY_A);
if(count($r) == 0)
return false;
foreach ( $r as $key => $val ){
$this->$key = stripslashes($val);
}
return true;
}
} // class
You are not retrieving the $_GET variable?
Assuming this line is where you think you are retrieving it:
if($cid != '') $params['category_id'] = $cid;
From you code that condition will always be false.
Correct use would be:
if($_GET['cid'] != '') $params['category_id'] = $_GET['cid'];

menu only showing the first child php

i have this code:
$Maxcount = 10;$count = 1;$query = "SELECT pages.paid,
pages.pag_desc,
pages.isActive,
pages.pag_pag_id,
pages.pag_type,
parent.pag_desc,";while ($count <= $Maxcount) {$query .= "parent".$count.".pag_desc AS parent".$count;if ($count != $Maxcount) {$query .= ", ";$count++;} $query .= " FROM pages ";$query .= "LEFT JOIN pages parent ON pages.pag_pag_id = parent.paid ";$count = 1;while ($count <= $Maxcount) { if ($count === 1) {
$query .= "LEFT JOIN pages parent".$count." ON parent.pag_pag_id = parent".$count.".paid "; }else {
$query .= "LEFT JOIN pages parent".$count." ON parent".($count-1).".pag_pag_id = parent".$count.".paid " }$count++;}$query .="LEFT JOIN tblgroups_rights_gri on pages.paid = tblgroups_rights_gri.gri_pag_id
INNER JOIN users_usergroups on tblgroups_rights_gri.gri_usrg_id = users_usergroups.gid
WHERE uid = '$uid' AND $where AND pages.isActive=1";
$result = $db->query($query);
if (mysqli_num_rows($result)) {
$menu = array();
while ($row = $result->fetch_assoc()) {
$menu[] = $row;
}
}
and for showing the menu i used this recursive function :
foreach($menu as $row){
$x[$row['parent3']][$row['parent2']][$row['parent1']][$row['pag_desc']]=$row['pag_desc'];
}function recursive($x, $out) { if (is_array($x)){
//$out .= "<ul>";
foreach($x as $i => $data) {
if ($i === '') {
$out = recursive($data, $out);
continue;
}
$out .= "<li>";
if (is_array($data)){
$out .= ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">';
}
else
{
$out .= '<a href="#">';
}
$out .= $i;
if (is_array($data)){
$out .= '<b class="caret"></b></a>';
}
else
{
$out .= "</a>";
}
if (is_array($data)){
$out .= "<ul class='dropdown-menu'>";
$out = recursive($data, $out);
$out .= "</ul></li>";
}
else
{
$out .= "</li>";
}
}
}
return $out;
}
the result is that its showing only the parent and the first child but in database the child have another child etc...
for example:
parent user have a child admin and admin have another child called editor
but its only showing user as a parent and admin as a child
any help please? thank u in advance

Create dynamic PHP / HTML category tree

I've figured out how to fetch each parent/child with ul > li structure but now I'm stuck, the sub category ul's need a class 'dropdown' but can't figure this out.
My goal is to create an infinite dynamic ul menu with Foundation nav markup.
PHP
function fetchCategoryTreeList($parent = 0, $user_tree_array = '') {
$isParent = '';
if (!is_array($user_tree_array))
$user_tree_array = array();
$sql = "SELECT id,name,parent FROM `categories` WHERE 1 AND parent=$parent ORDER BY id ASC" or die(mysql_error);
if (database_querySelect($sql,$rows)) {
$user_tree_array[] = "<ul>";
foreach ($rows as $row) {
if ($row["parent"] == "0") { $isParent = "has-dropdown"; }
$user_tree_array[] = "<li class='".$isParent."'>". $row["name"]. "</li>";
$user_tree_array = fetchCategoryTreeList($row["id"], $user_tree_array);
}
$user_tree_array[] = "</ul>";
}
return $user_tree_array;
Code to display cat's
<?php
$res = fetchCategoryTreeList();
foreach ($res as $r) {
echo $r;
}
?>
Output
<ul>
<li class='has-dropdown'>Golf Equipment</li>
<ul>
<li class=''>Manual Golf Trolleys</li>
<li class=''>Electric Golf Trolleys</li>
</ul>
<li class='has-dropdown'>Weight Training</li>
<ul>
<li class=''>Weight Benches</li>
<li class=''>Weights</li>
</ul>
</li>
</ul>
My be it help .. I dont know (need a class 'dropdown') for what... you can give the main ul a Class
$user_tree_array[] = "<ul class='main_ul'>";
and then you can use selector in css and jquery
.main_ul .has-dropdown ul {
}
function:
<?php
function fetchCategoryTreeList( $parent = 0, $user_tree_array = "" ) {
if ( ! is_array($user_tree_array))
$user_tree_array = array();
$sql = "SELECT id,name,parent ";
$sql .= "FROM `categories` ";
$sql .= "WHERE 1 AND parent = {$parent} ";
$sql .= "ORDER BY id ASC";
if ( database_querySelect( $sql, $rows) )
{
foreach ( $rows as $row )
{
$user_tree_array[] = "<li class='' >{$row["name"]}</li>";
if ( $parent != $row["id"] )
{
$user_tree_array[] = "<ul>";
$user_tree_array = fetchCategoryTreeList( $connection, $row["id"], $user_tree_array );
$user_tree_array[] = "</ul>";
}
else
{
$user_tree_array = fetchCategoryTreeList( $row["id"], $user_tree_array );
}
}
}
return $user_tree_array;
}
?>
Call:
<?php
$arrays = fetchCategoryTreeList();
foreach ( $arrays as $list)
{
echo $list;
}
?>

MYSQL in while array, print first result different from the others

I need the following, this is my function:
function i_iframe( $cadena ) {
$x = #mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
while ( $i = mysql_fetch_array( $x ) ) {
$option = adios( $i['idioma'] );
echo "<li>".$option."</li>";
}
#mysql_free_result( $x );
}
this code outputs:
<li>option name 1</li>
<li>option name 2</li>
<li>option name 3</li>
I need the outputs should be like this:
<li class="selected">option name 1</li>
<li>option name 2</li>
<li>option name 3</li>
the first result from thee loop should print <li class="selected"> and rest should be <li>
function i_iframe($cadena) {
$x=#mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
bool $first = true;
while($i=mysql_fetch_array($x)) {
$option = adios($i['idioma']);
if($first)
{
echo "<li class=\"selected\">".$option."</li>";
$first = false;
} else {
echo "<li>".$option."</li>";
}
}
#mysql_free_result($x);
}
Shoud do the trick
Call mysql_fetch_array once for your first element:
function i_iframe($cadena){
$x=#mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($i=mysql_fetch_array($x)){
$option = adios($i['idioma']);
echo '<li class="selected">', $option, '</li>';
while($i=mysql_fetch_array($x)){
$option = adios($i['idioma']);
echo "<li>".$option."</li>";
}
}
#mysql_free_result($x);
}
Now format your code:
function i_iframe($cadena) {
$x = #mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($i = mysql_fetch_array($x)) {
$option = adios($i['idioma']);
echo '<li class="selected">', $option, '</li>';
while($i = mysql_fetch_array($x)) {
$option = adios($i['idioma']);
echo '<li>', $option, '</li>';
}
}
#mysql_free_result($x);
}
Stop ignoring errors and use clearer names:
function i_iframe($cadena) {
$query = mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li class="selected">', $option, '</li>';
while($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li>', $option, '</li>';
}
}
mysql_free_result($x);
}
Escape your inputs where they’re used, especially if you weren’t already:
function i_iframe($cadena) {
$cadena = mysql_real_escape_string($cadena);
$query = mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li class="selected">', $option, '</li>';
while($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li>', $option, '</li>';
}
}
mysql_free_result($x);
}
Now stop using that deprecated extension and enjoy a life of PDO:
function i_iframe($cadena) {
global $db;
$query = $db->prepare('SELECT idioma FROM videos WHERE id_peli = :cadena LIMIT 3');
$query->execute([':cadena' => $cadena]);
$videos = $query->fetchAll(PDO::FETCH_OBJ);
$first = array_shift($videos);
echo '<li class="selected">', htmlspecialchars(adios($first->idioma)), '</li>';
foreach($videos as $video) {
echo '<li>', htmlspecialchars(adios($first->idioma)), '</li>';
}
}

Multi level selectbox

I have a problem with building multi level select box. I have category table with structure: id, parent_id, name. If parent_id = 0 it is top level. i don't know level depth, so it could be 2,3-5 levels.
How i can build it with on query "SELECT * FROM cats"
Result suppose to look like
<select>
<option>cat_name</option>
<option>--cat_name_l1</option>
<option>--cat_name_l1</option>
<option>----cat_name_l2</option>
<option>----cat_name_l2</option>
<option>cat_name</option>
</select>
Can You help me?
function _buildTree($data, $idParent, $indentSymbol, $level)
{
$cat = array();
foreach($data as $row){
if($row['parent_id'] == $idParent){
if($indentSymbol AND $level > 0){
$symbols = array_fill(0, $level, $indentSymbol);
$cat[$row['id']] = implode('', $symbols) . $row['name'];
}else{
$cat[$row['id']] = $row['name'];
}
$cat = $cat + _buildTree($data, $row['id'], $indentSymbol, $level++);
}
}
return $cat;
}
$result = mysql_query("SELECT * FROM cats");
$data = array();
while($row = mysql_fetch_assoc($result)){
$data[] = $row;
}
$select = '<select>';
foreach(_buildTree($data, 0, '-', 0) as $key=>$option){
$select .= '<option value=' . $key . '>' . $option . '</option>';
}
$select .= '</select>';
Try this for generating the required dropdown:
$result=mysql_query("SELECT * FROM cats");
$opt='<select>';
while($row=mysql_fetch_array($result))
{
$cat[$row['id']][0]=$row['parent_id'];
$cat[$row['id']][1]=$row['name'];
}
foreach($cat as $ct)
{
$level=0;
$temp=$ct;
while($temp[0]!=0)
{
$temp=$cat[$temp[0]];
$level++;
}
$opt.="<option>";
for($i=0;$i<$level;$i++)
{
$opt.="--";
}
$opt.=$ct[1];
$opt.="</option>";
}
$opt.="</select>";
Then, you can use this as:
echo $opt;

Categories