PHP Error Undefined variable: data - Loading more data using Codeigniter - javascript

I am making a load more data function when pressing a button via Codeigniter
I got this problem in-front of me
can't solve it
maybe the problem is the array doesn't passed to the view !!
Can you help ?
Javascript
<script type="text/javascript">
$(document).ready(function(){
var num_ads = <?=$num_ads?>;
var loaded_ads = 0;
$("#more_button").click(function(){
loaded_ads += 10;
$.get("e3lanat/get_ads/" + loaded_ads, function(data){
$("#Ebda2").append(data);
});
if(loaded_ads >= num_ads - 10)
{
$("#more_button").hide();
//alert('hide');
}
})
})
</script>
View
<div id="Ebda2">
<?php foreach ($data['latest_messages'] as $ads)
{
echo $ads->current_price;
echo '<div class="water">';
echo 'div class="eye-top">';
echo '<img class="img-responsive" src="public/images/9.jpg" alt="">';
echo '</div>';
echo '<div class="title-blue">';
echo '<h4><a href="#">';
echo $ads->name;
echo "</a></h4>";
echo "<p>";
echo $ads->description;
echo'</p>';
echo '</div>';
}
?>
</div>
<div id="more_button">
more
</div>
Controller
function __construct(){
parent::__construct();
$this->load->model('ads_model');
}
public function index(){
if($this->session->userdata('logged_in'))
{
$this->load->model('ads_model');
$data['num_messages'] = $this->ads_model->num_ads();
$data['latest_messages'] = $this->ads_model->get_ads();
$this->load->view('e3lanat_view', $data);
}
else
{
redirect('signin', 'refresh');
}
}
public function insert_rows()
{
$i = 0;
while($i < 50)
{
$i++;
$data = array('name' => 'name ' . $i, 'description' => 'description ' . $i,
'user_id' => 9, 'cat_id' => '1',
'current_owner_id' => 10, 'cat_id' => '1',
'initial_price'=> $i+10,
'current_price'=> $i+50,
'increase_price'=> $i+30
);
$this->db->insert('ads', $data);
echo $i. '<br />';
}
}
public function get_ads($offset)
{
$this->load->model('ads_model');
$data['latest_messages'] = $this->ads_model->get_ads($offset);
$this->load->view('e3lanat_view/get_ads', $data);
}
**Model**
class ads_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function get_ads($offset=0)
{
$this->db->order_by('ads_id', 'desc');
$query = $this->db->get('ads', 10, $offset);
return $query->result();
}
function num_ads()
{
$query = $this->db->count_all_results('ads');
return $query;
}
}
Thanks in Advance :)

You are trying to load the view instead of returning data.
In Controller:
public function get_ads($offset) {
$this->load->model('ads_model');
$data['latest_messages'] = $this->ads_model->get_ads($offset);
return json_encode($data['latest_messages']);
}
In View Javascript:
$.get("e3lanat/get_ads/" + loaded_ads, function(data){
console.log(data);
});
Now you need to iterate over the JSON and append your data to the view. See how to Loop through JSON object List and appending data to the view will go something like this:
$("#Ebda2").append('<div class="water"> ...every thing inside... </div>');

change this
<?php foreach ($data['latest_messages'] as $ads)
to
<?php foreach ($latest_messages as $ads)

Related

Insert multiple records in single checkbox in codeigniter?

I've two different table, 1st is draft_table, and 2nd is fix_table, each table have same fields ( product & price )
i want to make data from draft_table, can be save to fix_table with checkbox, so just selected data will save to fix_table.
i've code like this :
AJAX
<script>
$(function(){
$("a.paid").click(function(){
if(confirm("Are you sure want save this?"))
{
id_array=new Array()
i=0;
$("input.chk:checked").each(function(){
id_array[i]=$(this).val();
i++;
})
$.ajax({
url:'<?php echo base_url(); ?>fix_data/set',
data:"kode="+id_array,
type:"POST",
success:function(respon)
{
if(respon==1)
{
window.parent.location.reload(true);
}
}
})
}
return false;
})
})
</script>
Views :
<?php
foreach($data_get->result_array() as $dp)
{
?>
<tr><td><input type="checkbox" name="chk[]" class="chk" value="<?php echo $dp['id_draft']; ?>" /></td>
<td><?php echo $dp['product']; ?></td>
<td><?php echo $dp['price']; ?></td>
</td></tr>
<?php
}
?>
Controller :
public function set_stts()
{
if($this->session->userdata('logged_in')!="")
{
$id_get = $this->input->post('kode');
$dt = $this->db->get_where("tbl_draft",$id_get)->row();
$product = $dt->product;
$price = $dt->price;
if ($this->input->post('kode')) {
$query = $this->db->query("INSERT INTO tbl_fix (product,price) VALUES (".$product.",".$price.")");
}
if($query){
echo 1;
}
else{
echo 0;
}
}
else
{
header('location:'.base_url().'dashboard_item');
}
}
After i Click submit in draft form, nothing happen, is there anyone may help me with this case?
Thank you
maybe you can change your controller like this :
public function set()
{
if($this->session->userdata('logged_in')!="")
{
$id_get = $this->input->post('kode');
$quer = $this->db->query("select * from tbl_draft WHERE id IN (".$id_get.")");
if ($this->input->post('kode')) {
foreach($quer->result_array() as $dp)
{
$a = $dp['product'];
$b = $dp['price'];
$query = $this->db->query("INSERT INTO tbl_fix (product,price) VALUES
('".$a."','".$b."')");
}
}
if($query){
echo 1;
}
else{
echo 0;
}
}
else
{
header('location:'.base_url().'dashboard_item');
}
}

ajax return data in database out of order

function selectDiv() {
<?php
$stmt = $conn->prepare("SELECT id, name FROM data");
$stmt->execute();
$stmt->bind_result($id ,$name);
while ($stmt->fetch()) {?>
add (<?php echo "'$id'";?>,<?php echo "'$name'";?>);
<?php } ?>
}
function add (id , name) {
$.post("type.php",{id:id, name:name}, function (data) {
add2 (id, name, data);
});
document.getElementById("div").innerHTML += name + "<br>";
}
function add2 (id , name, type) {
document.getElementById("div").innerHTML += name + "|"+ type + "<br>";
}
I retrieve data from data base using $.post and on each turn I call function add to append a certain div. the problem is that the data comes out of order. How can I fix it? please help
Try this if is possible:
function selectDiv() {
<?php $stmt = $conn->prepare("SELECT id , name, t2.type FROM data INNER JOIN t2 ON join_condition1”);
$stmt->execute();
$stmt->bind_result($id ,$name);
while($stmt->fetch()) {?>
add (<?php echo "'$id'";?>, <?php echo "'$name’”;?>, <?php echo "'$type’”;?>);
<?php } ?>
}
function add (id , name, type){
document.getElementById("div").innerHTML += name + "<br>" + "|"+ type + "<br>";
}

Laravel "MethodNotAllowedHttpException in RouteCollection.php line 219"

I'm just wondering right now why this particular table I am interacting with the database returns an error like the above subject where in fact I am just using the same mechanism from other table. I am refering to my table "Inventory". My store method just simply returns an error stated in the subject above and I have checked all the parameters in controller and in my model and it seems there is no error. Can somebody help me spot the error here. Please help. Here are my codes.
InventoryController.php
public function store( Request $request ){
$prod_ids = $request['product_ids'];
$purchase_order_ids = $request['purchase_order_ids'];
$purchase_ids = $request['purchase_ids'];
$quantity = $request['purchaseQuantity'];
for($i = 0; $i < count($prod_ids); $i++){
Inventory::create([
'product_id' => $prod_ids[$i],
'purchase_order_id' => $purchase_order_ids[$i],
'purchase_id' => $purchase_ids[$i],
'quantity' => $quantity[$i]
]);
}
return json_encode(array('notify' => "Success"));
}
My model => Inventory.php
<?php
namespace App\Inventory;
use Illuminate\Database\Eloquent\Model;
class Inventory extends Model
{
protected $fillable =
['product_id','purchase_order_id','purchase_id','quantity'];
}
My Javascript:
$("#purchase_toInventoryID").click(function(e){
e.preventDefault();
var array_prodID = <?php echo json_encode(isset($array_prodID) ? $array_prodID : 0); ?>;
var array_poID = <?php echo json_encode(isset($array_poID) ? $array_poID : 0); ?>;
var array_purchaseID = <?php echo json_encode(isset($array_purchaseID) ? $array_purchaseID : 0); ?>;
var array_purchaseQuantity = <?php echo json_encode(isset($array_purchaseQuantity) ? $array_purchaseQuantity : 0); ?>;
$.post("{{ url('saveToInventory') }}", {
'product_ids': array_prodID,
'purchase_order_ids': array_poID,
'purchase_ids':array_purchaseID,
'purchaseQuantity': array_purchaseQuantity
}, function(data){
if(data.notify == "Success"){
console.log(data.notify);
}else{
console.log(data.notify);
}
}, 'json');
});
My routes.php
Route::post('saveToInventory', 'Inventory\InventoryController#store');

Ajax in codeigniter for changing only div in view

Hello, I want to be able to change only part of the div in my view i
have tried to write the codes but they are not working perfectly. how
it works is when i click the button it suppose to sent the value to a
js then using js to sent data to the controller.
SCRIPT:
function getValue(datas){
alert(datas) ;
var mydiv = datas;
$.Ajax({
type:"post",
url:<?php echo base_url('/mama/load_view/');?>
data:{"mydiv":mydiv},
success:function(data){
$('#mang_server').html(data);
}
});
}
PHP:
foreach($student_prof as $row){
$gen = "GeneralInformation";
echo '<input type="submit" value="GeneralInformation" class="btn btn-primary active" name="general" id="submit" onclick="getValue("'.$gen.'")">';
echo '</div>';
}
The Controller code
public function load_view(){
$data1 = $this->input->post('mydiv');
if($data1 == "GeneralInformation"){
$this->load->view('student/profile_view');
}
else {
$this->load->view('student/profile_view');
}
}
use jquery load function.
function getValue(datas){
var mydiv = datas;
url = "<?php echo base_url('/mama/load_view');?>/" + mydiv;
$('#mang_server').load(url);
}
Controller
public function load_view($data1){
if($data1 == "GeneralInformation"){
$this->load->view('student/profile_view');
}
else {
$this->load->view('student/profile_view');
}
}
to load your view in div using ajax use echo
echo $this->load->view('student/profile_view', $data1, TRUE);
public function load_view(){
$data1 = $this->input->post('mydiv');
if($data1 == "GeneralInformation"){
echo $this->load->view('student/profile_view', $data1, TRUE);
}
else {
echo $this->load->view('student/profile_view', $data1, TRUE);
}
}
change you php code to this
foreach($student_prof as $row){
$gen = "GeneralInformation";
$output .='<div>';
$output .= '<input type="submit" value="GeneralInformation" class="btn btn-primary active" name="general" id="submit" onclick="getValue("'.$gen.'")">';
$output .= '</div>';
}
echo $output;

get < image> src php to display in html

This is my javascript code :
$(document).ready(function() {
$('.aire-force,.army').click(function() {
var storedData;
var qString = 'categorie=' +$(this).text();
$.post('getimage.php', qString, processResponse);
});
function processResponse(data) {
$('.results').html(data);
storedData = data;
alert(storedData);
document.getElementById('test').innerHTML = test;
}
});
and this my file PHP (getimage.php):
<?php
$con = mysql_connect("localhost","root","");
if(!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("DB", $con);
$categorie = $_POST['categorie'];
$q = "SELECT title from designs WHERE categorie='$categorie'";
$r = mysql_query($q);
while( $array = mysql_fetch_array($r)){
echo $array['title'];
}
?>
and i need to get the tile of image in my code HTML :
require_once('getimage.php');
<img src="images/ echo $array['title']; />
require_once('getimage.php');
< img src="images/ echo $array['title']; />
Should be replaced by:
<?php include('getimage.php'); ?>
<img src="images/<?php echo $array['title']; ?>" />
This should work.. Save this html file also as a .php ;)
document.getElementById('test').innerHTML = test;
You never set test variable.

Categories