Search function doesn't show anything - php

I'm trying for a search function, I made a public function in a file called Topic, and I tried calling the method in the index.php, however it doesn't show anything, I think the problem lies with the while statement, but i'm not entirely sure.
Topic.php
class Topic{
public function searchPosts($postTags)
{
$conn = Db::getInstance();
$statementSearch = $conn->prepare("SELECT * FROM topics INNER JOIN posttopic ON topics.topicID=posttopic.topicID INNER JOIN posts ON posttopic.postID=posts.postID WHERE naam = :naam");
$statementSearch->bindValue(":naam", $postTags);
return $statementSearch->execute(array());
}
}
Index.php
spl_autoload_register(function ($class) {
include_once("classes/" . $class . ".php");
});
$_SESSION['KEYWORD'] = array();
$postArray = array();
//$allResults2 = array();
if (isset($_POST['Find'])) {
if (!empty($_POST['Find'])) {
$searchTopic = new Topic();
$postTags = $_POST['naam'];
$searchTopic->searchPosts($postTags);
while ($row = $statementSearch->fetch(PDO::FETCH_ASSOC)) {
$_SESSION['KEYWORD'][] = $row['postImageUrl'];
$postArray[] = $row['postID'];
}
}
if (count($_SESSION['KEYWORD']) === 0) {
$error = "Sorry no results!";
}
}
This is the html where it should be printed.
<?php
foreach (array_combine($_SESSION['KEYWORD'], $postArray) as $imageLink => $i) {
echo "<a href='./pin.php?postid=$i' ><img src='" . $imageLink . "'</a>";
}
?>

Related

?url= does not work

I am coding for a site which use admin panel and member panel both panels files are located in two separate folders named master and member. Both have main.php file which are header files from those files i redirect to other files, Both main.php use same function file to redirect url by ?url= .
Now problem is admin main.php redirect to correct url but member main.php returns back without showing errors.
function.php is
<?php
function logout($destinationPath)
{
if(count($_SESSION))
{
foreach($_SESSION AS $key=>$value)
{
session_unset($_SESSION[$key]);
}
session_destroy();
}
echo "<script language='javaScript' type='text/javascript'>
window.location.href='".$destinationPath."';
</script>";
}
function validation_check($checkingVariable, $destinationPath)
{
if($checkingVariable == '')
{
echo "<script language='javaScript' type='text/javascript'>
window.location.href='".$destinationPath."';
</script>";
}
}
function realStrip($input)
{
return mysql_real_escape_string(stripslashes(trim($input)));
}
function no_of_record($table, $cond)
{
$sql = "SELECT COUNT(*) AS CNT FROM ".$table." WHERE ".$cond;
$qry = mysql_query($sql);
$rec = mysql_fetch_assoc($qry);
$count = $rec['CNT'];
return $count;
}
//drop down
function drop_down($required=null, $text_field, $table_name, $id, $name, $cond, $selected_id=null)
{
$qry = mysql_query("SELECT $id, $name FROM $table_name WHERE $cond ORDER BY $name ASC");
$var = '';
if(mysql_num_rows($qry)>0)
{
$var = '<select id="'.$text_field.'" name="'.$text_field.'" '.$required.'>';
$var .='<option value="">--Choose--</option>';
while($r = mysql_fetch_assoc($qry))
{
$selected = '';
if($selected_id==$r[$id]){
$selected = 'selected="selected"';
}
$var .='<option value="'.$r[$id].'" '.$selected.'>'.$r[$name].'</option>';
}
$var .='</select>';
}
echo $var;
}
function uploadResume($title,$uploaddoc,$txtpropimg)
{
$upload= $uploaddoc;
$filename=$_FILES[$txtpropimg]['name'];
$fileextension=strchr($filename,".");
$photoid=rand();
$newfilename=$title.$photoid.$fileextension;
move_uploaded_file($_FILES[$txtpropimg]['tmp_name'],$upload.$newfilename);
return $newfilename;
}
function fRecord($field, $table, $cond)
{
$fr = mysql_fetch_assoc(mysql_query("SELECT $field FROM $table WHERE $cond"));
return $fr[$field];
}
function get_values_for_keys($mapping, $keys) {
$output_arr = '';
$karr = explode(',',$keys);
foreach($karr as $key) {
$output_arr .= $mapping[$key].', ';
}
$output_arr = rtrim($output_arr, ', ');
return $output_arr;
}
function getBaseURL() {
$isHttps = ((array_key_exists('HTTPS', $_SERVER)
&& $_SERVER['HTTPS']) ||
(array_key_exists('HTTP_X_FORWARDED_PROTO', $_SERVER)
&& $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
);
return 'http' . ($isHttps ? 's' : '') .'://' . $_SERVER['SERVER_NAME'];
}
function request_uri()
{
if ($_SERVER['REQUEST_URI'])
return $_SERVER['REQUEST_URI'];
// IIS with ISAPI_REWRITE
if ($_SERVER['HTTP_X_REWRITE_URL'])
return $_SERVER['HTTP_X_REWRITE_URL'];
$p = $_SERVER['SCRIPT_NAME'];
if ($_SERVER['QUERY_STRING'])
$p .= '?'.$_SERVER['QUERY_STRING'];
return $p;
}
preg_match ('`/'.FOLDER_NAME.'(.*)(.*)$`', request_uri(), $matches);
$tableType = (!empty ($matches[1]) ? ($matches[1]) : '');
$url_array=explode('/',$tableType);
?>
In main.php
<li><a href='?url=epin/used.php'><span>Used e-Pin</span></a></li>
is used to redirect url
http://localhost/abcd.biz/secure/master/main.php?url=epin/used.php is working
and http://localhost/abcd.biz/secure/member/main.php?url=epin/used.php is not working
Both main.php files are using same function file so I can't get the reason please help to get out of it, thanks in advance, try to clarify it also.
I am not using any htaccess file

Passing variables from one class to an other in PhP

Hello I am trying to build my first restful web service and im using the instruction from lorna jane mitchell blog.
If the req comes through this Url : http://localhost:8888/lorna/index.php/tree/getpath?node_id=75
i call the function getpath passing node_id
The function get path looks like this :
class NestedSet
{
public function getPath($id) {
$sql = "SELECT p." . $this->pk . ", p." . $this->name . " FROM ". $this->table . " n, " . $this->table . " p WHERE n.lft BETWEEN p.lft AND p.rgt AND n." . $this->pk ." = " . $id . " ORDER BY p.lft;";
$result = $this->db->query($sql);
if ($result->num_rows == 0) {
return $this->error(1, true);
}
$path = array();
$i = 0;
while ($row = $result->fetch_assoc()) {
$path[$i] = $row;
$i++;
}
return $path;
}
}
Now i want to pass this variable $path to the class JsonView that looks like this :
class JsonView extends ApiView {
public function render($path) {
header('Content-Type: application/json; charset=utf8');
echo json_encode($path);
return true;
}
}
class ApiView {
protected function addCount($data) {
if(!empty($data)) {
// do nothing, this is added earlier
} else {
$data['meta']['count'] = 0;
}
return $data;
}
}
Any Idea on how can I pass the variable $path or any other variable through this JsonView Class.
Thank you very much for your time :)
UPDATE This is the code for creating the nested class object
public function getAction($request) {
$data = $request->parameters;
if(isset($request->url_elements[2])) {
switch ($request->url_elements[2]) {
case 'getpath':
$id = $data['node_id'];
$nested = new NestedSet();
$nested->getPath($id);
$api = new JsonView();
$api->render($path);
break;
default:
# code...
break;
}
} else {
$nested = new NestedSet();
echo $nested->treeAsHtml();
}
}
Just create object of JsonView and then call the function render using that object.
$api = new JsonView;
$api->render($path);

how to return parent full name with child id

I have three functions that returns all parents for specific child with user_id with recursive query.
All these functions works good ..but the problem begins when I start use foreach loop to return multiple user parent names in function
merge_comments_data..
note : t_relation means = parent_id
class News extends front_end {
public $parents_names;
function merge_comments_data($related_comments) {
foreach ($related_comments as $comment) {
$full_name = $this->get_parents_names($comment['cn_visitor_id']);
}
echo "<pre>";
print_r($names);
echo "</pre>";
exit;
}
//// all these function to get full parent names by user id
function get_parents_names($user_id = 0) {
$this->user_parent_name($user_id);
echo $this->parents_names;
}
function user_parent_name($user_id = 0) {
// clear the variable at first
$result = $this->get_parents($user_id);
if (is_object($result)) {
$this->parents_names .= ' ' . $result->t_name . ' ';
if ($result->t_relation != 0) {
$this->user_parent_name($result->t_relation);
}
}
}
public function get_parents($user_id = 0) {
$result = $this->db->query("SELECT * FROM d_tree where t_id = '$user_id'");
if (is_object($result->row())) {
$result = $result->row();
} else {
$result = '';
}
return $result;
}

Pagination links in codeigniter 2 went to empty result

I tried to create pagination in search result. I got whole search result and correct number of pagination links, but when tried to go to next page search result went blank. 1st page should be fine. But it dosen't shows any error.
Here is the code:
Controller function
public function staffsearch() {
if ($this->session->userdata('logged_in') == FALSE) {
$this->index();
} else {
$data['action'] = site_url('user/staffsearch/');
$search_by = $this->input->post('search_by');
$keyword = $this->input->post('keyword');
if (!empty($search_by) && !empty($keyword)) {
$uri_segment = 3;
$offset = $this->uri->segment($uri_segment);
$staff_details = $this->User_Model->get_staff_search_result($search_by, $keyword, $this->limit, $offset)->result();
$query = $this->db->query("SELECT * FROM `staff` WHERE `" . $search_by . "` LIKE '%" . $keyword . "%'");
$count = $query->num_rows();
$this->load->library('pagination');
$config['base_url'] = site_url('user/staffsearch/');
$config['total_rows'] = $count;
$config['per_page'] = $this->limit;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
//$staff_details = $this->User_Model->get_staff_search_result($search_by, $keyword)->result();
$this->load->library('table');
$this->table->set_empty(' ');
$this->table->set_heading('S/No', 'Image', 'Name', 'Office', 'Phone');
$i = 0;
foreach ($staff_details as $staff) {
if ($staff->Office) {
$id = $staff->Office;
$staff_office = $this->User_Model->get_office_by_id($id)->row();
$office = $staff_office->building . ' ' . $staff_office->level . '-' . $staff_office->unit;
} else {
$office = '';
}
if ($staff->Photo_small == '') {
$pic = 'unavailable.jpg';
} else {
$pic = $staff->Photo_small;
}
$this->table->add_row(++$i, '<image src="' . base_url() . 'people/' . $pic . '" width="50" height="50">', anchor('user/staffdetails/' . $staff->people_id, $staff->Name), $office, $staff->Phone);
}
$data['title'] = 'Search Result';
$data['table'] = $this->table->generate();
}
$this->load->view('search_staff', $data);
}
}
Model function:
function get_staff_search_result($fiels, $key,$limit = 10, $offset = 0) {
if ($fiels == 'Name') {
$this->db->like('Name', $key);
} elseif ($fiels == 'Staffnumber') {
$this->db->like('Name', $key);
} else {
$this->db->like('Appointment', $key);
}
$this->db->order_by('Name', 'asc');
return $this->db->get($this->tbl_staff,$limit, $offset);
}
View:
<div>
<h2><?php if (isset($title)) { echo $title;} ?></h2>
<?php if (isset($table)) { echo $table; } ?>
<?php if (isset($pagination)) { echo $pagination;} ?>
</div>
The links created by the pagination class won't contain the 'search_by' and 'keyword' criteria and they certainly won't be in the post variables as when you click a page link you are doing an HTTP GET.
You need to think about either storing the criteria in somewhere like the user's session so that it can be reused for each page request or look to use the Pagination library from CodeIgniter 3 which supports adding items (such as your two search terms) as query string parameters which you could then also check for.
Details about the CodeIgniter 3 Pagination class can be found here.
Not tried this myself but you should also be able to amend the URL for the pagination links so they read:
site_url('user/staffsearch/') . $search_by .'/' . $keyword;
You could then read the individual URL segments to retrieve the search terms.

Template engine {var} doesnt work

I have made a template system but the {var} doesnt output the worth.
It just output {var}.
Here is my template class:
<?php
class Template {
public $assignedValues = array();
public $tpl;
function __construct($_path = '')
{
if(!empty($_path))
{
if(file_exists($_path))
{
$this->tpl = file_get_contents($_path);
}
else
{
echo 'Error: No template found. (code 25)';
}
}
}
function assign($_searchString, $_replaceString)
{
if(!empty($_searchString))
{
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
}
}
function show()
{
if(count($this->assignedValues) > 0)
{
foreach ($this->assignedValues as $key => $value)
{
$this->tpl = str_replace('{'.$key.'}', $value, $this->tpl);
}
}
echo $this->tpl;
}
}
?>
And here is what I execute on the index:
<?php
require_once('inc/classes/class.template.php');
define('PATH', 'tpl');
//new object
$template = new Template(PATH.'/test.tpl.html');
//assign values
$template->assign('title', 'Yupa');
$template->assign('about', 'Hello!');
//show the page
$template->show();
?>
I really need some help, if you can help I'd would be very grateful.
Instead of line:
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
You should have:
$this->assignedValues[$_searchString] = $_replaceString;
and it will work.
Of course I assume that inside your template file you have content:
{title} {about}
You should change
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
to this:
$this->assignedValues["{".$_searchString . "}"] = $_replaceString ;
this will only replace your keywords with values.

Categories