So I have two MySql searches on my jQuery mobile page but they do not work together.
Here is the code:
my index.php code:
<?php
require "functions.php";
$posts = getAllPosts();
$posts = getSearchPosts();
?>
<?php
for( $p = 0; $p < count( $posts ); $p++ )
{
?>
<div class="ui-corner-all custom-corners">
<div id="searchpost">
<?php echo $posts[$p]["search_role"];?> <!-- using getSearchPosts -->
<?php echo $posts[$p]["search_genre"];?>
<?php echo $posts[$p]["user_first_name"];?> <!-- using getAllPosts -->
<?php echo $posts[$p]["user_last_name"];}?>
</div>
</div>
Functions.php:
function getSearchPosts()
{
require "config.php";
$posts = $c->query ( "SELECT * FROM posts" );
if ( $posts->num_rows > 0 )
{
while( $row = $posts2->fetch_assoc() )
{
$postData[] = array( "user_id" => $row[ "user_id" ], "search_role" => $row[ "search_role" ], "search_genre" => $row[ "search_genre" ] );
}
} else {
return "No Data";
}
return $postData;
}
function getAllPosts()
{
require "config.php";
$posts = $c->query ( "SELECT * FROM users" );
if ( $posts->num_rows > 0 )
{
while( $row = $posts->fetch_assoc() )
{
$postData[] = array( "user_id" => $row[ "user_id" ], "user_first_name" => $row[ "user_first_name" ], "user_last_name" => $row[ "user_last_name" ] );
}
} else {
return "No Data";
}
return $postData;
}
I assume I am to change the $posts on one function to something else such as $posts -> $search or something but it does not echo anything after changing. What can I do to have both searches done at the same time?
Do them as two separate loops. It makes no sense to show them both in the same DIVs, since there's no correspondence between the results of the two queries.
$posts = getSearchPosts();
foreach ($posts as $post) {
?>
<div class="ui-corner-all custom-corners">
<div id="searchpost">
<?php
echo $post['search_role'];
echo $post['search_genre'];
?>
</div>
</div>
<?php
}
$users = getAllPosts();
foreach ($users as $user) {
?>
<div class="ui-corner-all custom-corners">
<div id="searchpost">
<?php
echo $user['user_first_name'];
echo $post['user_last_name'];
?>
</div>
</div>
<?php
}
Related
I have two html / css content. One of them floating left, the other one floating right.
I wanna pull data from my database, then putting them in a order. Left, right, left, right.
$services = pullservices();
while ($service = mysqli_fetch_object($services)){
for ($i=1; $i<=mysqli_num_rows($services); $i++) {
if ($i % 2 == 0): ?>
Left HTML Content
<?php else: ?>
Right HTML Content
<?php
endif;
}
}
?>
I have 6 items on db. But that code keeps looping one data for 6 times and it doing that for all the data on db.
That is the pullservices function.
function pullservices()
{
global $connect;
$products_query_string = "SELECT
id,
title_tr AS title,
text_tr,
short_text_tr,
image,
icon
FROM services WHERE status = 1 ORDER BY ordering , title_tr";
$products_query_run = mysqli_query($connect, $products_query_string);
return $products_query_run;
You can do this simply by using a flag.
(pseudo code):
$services = pullservices();
$flag=0;
while ($service = mysqli_fetch_object($services)){
if ($flag==0): ?>
Left HTML Content
<?php $flag=1; ?>
<?php else: ?>
Right HTML Content
<?php $flag=0; ?>
<?php
endif;
}
?>
One way you might be able to do this would be to split the data from the recordset and call it later in the html like this
$services = pullservices();
$i=0;
$data=array(
'left' =>array(),
'right' =>array()
);
while( $rs = mysqli_fetch_object( $services ) ){
$key = $i % 2 == 0 ? 'right' : 'left';
/* add the real data rather than placeholder data */
$data[ $key ][]=sprintf('%s HTML Content %d', ucfirst( $key ), $i );
$i++;
}
<div id='left'>
<?php
echo implode( PHP_EOL, $data['left'] );
?>
</div>
<div id='right'>
<?php
echo implode( PHP_EOL, $data['right'] );
?>
</div>
I have a script that is supposed to return values from a mysql tables based on search inputs. This script is composed of two files.
search.php
<?php
if ( isset( $_GET['s'])) {
require_once( dirname( __FILE__ ) . '/class-search.php' );
$search = new search();
$search_term = $GET['s'];
$search_results = $search->search($search_term);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<h1>Search</h1>
<div class="search-form">
<form action="" method="get">
<div class="form-field">
<label for="search-field">Search</label>
<input type="search" name="s" placeholder="Search by name" results="5" value="<?php echo $search_term; ?>">
<input type="submit" value="Search">
</div>
</form>
</div>
<?php if ( $search_results ) : ?>
<div class="results-count">
<p><?php echo $search_results['count']; ?> results found</p>
</div>
<div class="results-table">
<?php foreach ( $search_results['results'] as $search_result ) : ?>
<div class="result">
<p><?php echo $search_result->title; ?></p>
</div>
<?php endforeach; ?>
</div>
<div class="search-raw">
<pre><?php print_r($search_results); ?></pre>
</div>
<?php endif; ?>
</body>
and class-search.php
<?php
class search {
private $mysqli;
public function __construct() {
$this->connect();
}
private function connect() {
$this->mysqli = new mysqli('HOST', 'USERNAME', 'PASSWORD', 'DATABASE' );
}
public function search($search_term) {
$sanitized = $this->mysqli->query("
SELECT * FROM `Apple`
FROM search
WHERE Last_Name LIKE '%{$sanitized}%'
");
if ( ! $query->num_rows ) {
return false;
}
while( $row = $query->fetch_object() ) {
$rows[] = $row;
}
$search_results = array(
'count' => $query->num_rows,
'results' => $rows,
);
return $search_results;
}
}
?>
Within my database I have two tables, but I'm only interested in searching the content of one (Apple). Can somebody help me? I can't seem to make this work. No results are returned no matter what I search. As of now I'm only using the Last_Name criteria, but I'd like to add others. Here's a link to the screenshot of my table http://imgur.com/a/H3DnG.
I'd really appreciate any feedback possible. Thank you.
If you check it again,
In search method you're passing $search_term as argument but in the query you're using $sanitized which doesn't exists until the query is executed.
You're result set is in $sanitized but you're checking $query for num_rows which don't even exists. Also, you're returning false in that method so you're not able to identify the actual problem.
public function search($search_term) {
$sanitized = $this->mysqli->query("
SELECT * FROM `Apple`
FROM search
WHERE Last_Name LIKE '%{$search_term}%'
");
if ( ! $sanitized->num_rows ) {
//return false;
retrun [];
}
$rows = [];
while( $row = $sanitized->fetch_object() ) {
$rows[] = $row;
}
$search_results = array(
'count' => $query->num_rows,
'results' => $rows,
);
return $search_results;
}
In connect method, add this which will tell whether its getting connected to database or not.
if ($this->mysqli->connect_errno) {
printf("Connect failed: %s\n", $this->mysqli->connect_error);
exit();
}
I have a list containing tags with id name and status in a table and in my Model TagsStandard I have this:
const STATUS_APPROVED = 1;
const STATUS_DISABLED= 0;
public $id;
public $name;
public $status;
public static function getAllTag()
{
$tags = TagsStandard::find(array(
"status = 'STATUS_APPROVED'",
"order" => "id"
));
}
So with this I am getting all the tags. Now at Controller in indexAction I have:
$tags = TagsStandard::getAllTag();
if ($tags) {
$this->view->tags = $tags;
$this->view->name = array("name" => $tags->name->toArray());
}
And in the index I have:
<select id='user-skills-input' class="select-chosen" type="text" data-role="tagsinput" value="" multiple>
<?php if(count($tags) > 0): ?>
<?php foreach($tags->items as $idx => $tag):
echo "<option value='" . $tag->id . "'> " . $tag->name . "</option>" ?>
<?php endforeach; ?>
<?php endif; ?>
</select>
The values are not showing up in the options and so can anyone help me with this?
<?php
echo \Phalcon\Tag::select(array(
"user-skills-input",
TagsStandard::find(array(
"status = 'STATUS_APPROVED'",
"order" => "id"
)),
"using" => array("id", "name")
);
Phalcon Tags#select
I want to put this in a loop because it needs to be repeated 6 times, is it true that using variable variables is a bad practice? Do I need associative arrays?
Basically the 'c1' inside the variable needs to progressively change into 'c2', 'c3'...etc
<?php if ($pm_c1_djwd !== '') { ?>
<div>
<span style="width:<?php echo $pm_width_c1;?>%"></span>
<span><?php echo $pm_description_c1; ?></span>
</div>
<?php } ?>
Many Thanks
Why not try regular arrays?
<?php
// Warning: Typed raw in the textarea
$pm_width = array(100, 100, 100, 100, 100, 100);
$pm_description = array(
"Gizmo",
"Doodad",
"Widget",
"Dohicky",
"Thing-me-a-bob",
"Marvelous toy my father gave to me."
);
$pm_c1_djwd = "Snod";
if ($pm_c1_djwd !== '') {
for ($i = 0; $i < count($pm_description); $i++) {
$width = $pm_width[$i];
$desc = $pm_description[$i];
echo "<div>";
echo "<span style='width:${width}%'>$desc</span>";
echo '</div>';
echo PHP_EOL;
}
}
?>
Instead of having a variable for each field like $pm_description_c1, ..c2 and so on, put them in an associative array:
$pms = array(
array('description' => 'your description', 'width' => '123px', 'djwd' = 'what'),
array('description' => 'Second item', 'width' => '123px', 'djwd' = '')
);
Then loop through them:
<?php
foreach ($pms as $pm) {
if ($pm['djwd' !== '') {
?>
<div>
<span style="width:<?php echo $pm['width'];?>%"></span>
<span><?php echo $pm['description']; ?></span>
</div>
<?php
}
}
?>
You can use variable variables for the thing you are asking...
<?php
$i = 0;
while( $i < 6 ){
$i ++;
$variable = "pm_c".$i."_djwd";
$variable2 = "pm_description_c".$i."";
if (isset($$variable) && $$variable != '') { ?>
<div>
<span style="width:<?php echo $$variable;?>%"></span>
<span><?php echo $$variable2; ?></span>
</div>
<?php }
}
I'm learning code igniter and I'm currently trying to create a shopping cart that works over multiple tables. I can make it so it works using 1 table but when I try the JOIN method it always gives "Product does not exist".
The model I'm using is:
class Cart_model extends CI_Model {
function tvs(){
$query = $this->db->get('tvs');
return $query->result_array();
}
function computers(){
$query = $this->db->get('computers');
return $query->result_array();
}
function laptops(){
$query = $this->db->get('laptops');
return $query->result_array();
}
function validate_update_cart(){
$total = $this->cart->total_items();
$item = $this->input->post('rowid');
$qty = $this->input->post('qty');
for($i=0;$i < count($item);$i++)
{
$data = array(
'rowid' => $item[$i],
'qty' => $qty[$i]
);
$this->cart->update($data);
}
}
function validate_add_cart_item(){
$id = $this->input->post('product_id'); // Assign posted product_id to $id
$qty = $this->input->post('quantity'); // Assign posted quantity to $cty
$this->db->select('*');
$this->db->from('tvs');
$this->db->join('computers', 'computer.id = tv.id and computer.name = tv.name and computer.price = tv.price');
$this->db->join('laptops', 'laptops.id = tv.id and laptops.name = tv.name and laptops.price = tv.price');
$this->db->where('tvs.id', $id);
$query = $this->db->get();
return $query->result();
}
foreach($this->cart->contents() as $item) {
if($item['id'] == $id) {
$data = array(
'rowid' => $item['rowid'],
'qty' => $item['qty'] + $qty
);
$this->cart->update($data);
return TRUE;
}
}
$row = $query->row();
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $row->price,
'name' => $row->name
);
this->cart->insert($data);
return TRUE;
{
}else{
return FALSE;
}
}
}
This is the item list:
<ul class="products">
<?php foreach($TV as $p): ?>
<li>
<h1>
<a href="<?php echo current_url();?>/
<?php echo $p['link']; ?>" />
<?php echo $p['name']; ?>
</a>
</h1>
<img src="<?php echo base_url(); ?>assets/image/products/<?php echo $p['image']; ?>" alt="<?php echo $p['name']; ?>" />
<h3>
<?php echo $p['description']; ?>
</h3>
<small>£<?php echo $p['price']; ?></small>
<?php echo form_open('/testcart/home/add_cart_item'); ?>
<fieldset>
<label>Quantity</label>
<?php echo form_input('quantity', '1', 'maxlength="2"'); ?>
<?php echo form_hidden('product_id', $p['id']); ?>
<?php echo form_submit('add', 'Add to Cart'); ?>
</fieldset>
<?php echo form_close(); ?>
</li>
<?php endforeach;?>
</ul>`
Controller:
function add_cart_item(){
if($this->cart_model->validate_add_cart_item() == TRUE){
if($this->input->post('ajax') != '1'){
redirect('');
}else{
echo 'true';
}
}
}
The javascript
$(document).ready(function() {
var link = baseurl;
$("ul.products form").submit(function() {
// Get the product ID and the quantity
var id = $(this).find('input[name=product_id]').val();
var qty = $(this).find('input[name=quantity]').val();
$.post(link + "home/add_cart_item", { product_id: id, quantity: qty, ajax: '1' },
function(data){
if(data == 'true'){
$.get(link + "home/show_cart", function(cart){
$("#cart_content").html(cart);
});
}else{
alert("Product does not exist");
}
});
return false;
});
$(".empty").live("click", function(){
$.get(link + "home/empty_cart", function(){
$.get(link + "home/show_cart", function(cart){
$("#cart_content").html(cart);
});
});
return false;
});
});
I'm not too sure where it's going wrong as I'm relatively new to all this, whether it's the Javascript or the PHP.
This is your error free method. You were making mistake selecting from table tvs and using tv in joins
function validate_add_cart_item()
{
$id = $this->input->post('product_id'); // Assign posted product_id to $id
$qty = $this->input->post('quantity'); // Assign posted quantity to $cty
$this->db->select('*');
$this->db->from('tvs as tv');
$this->db->join('computers', 'computer.id = tv.id and computer.name = tv.name and computer.price = tv.price');
$this->db->join('laptops', 'laptops.id = tv.id and laptops.name = tv.name and laptops.price = tv.price');
$this->db->where('tv.id', $id);
$query = $this->db->get();
return $query->result();
}