• 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to retrive data from database and display

#1
hi,

I am beginner, below is my code 

<?php namespace App\Controllers;
use App\Models\product

class Product extends BaseController
{
public function post()
{
$data = $userModel->findAll();
return view('home',$data);
}

//--------------------------------------------------------------------
}


can you please tell me what is wrong?

error message is "Undefined variable: data"

how to retrieve data and display in view?
Reply

#2
You did not call the model function this code is totally wrong. Actually I did not know your model but you trying to use userModel you need to call out the user model

$userModel = new UserModel();

Then you need to call out the model via namespace
use App\Models\UserModel;
Reply

#3
(07-08-2020, 12:44 AM)seunex Wrote: You did not call the model function this code is totally wrong. Actually I did not know your model but you trying to use userModel you need to call out the user model

$userModel = new UserModel();

Then you need to call out the model via namespace
use App\Models\UserModel;
yes i was about to reply saying your not giving us enough information . Also really we need to see how you are doing  allowed fields if using CI model and whether your database is set up. I recommend sqlite for easy start can help with that if required
Reply

#4
(07-08-2020, 12:44 AM)seunex Wrote: You did not call the model function this code is totally wrong. Actually I did not know your model but you trying to use userModel you need to call out the user model

$userModel = new UserModel();

Then you need to call out the model via namespace
use App\Models\UserModel;

Thank you so much.
kindly see below code;


model: Product.php
Code:
<?php namespace App\Models;

use CodeIgniter\Model;

class Product extends Model
{
    protected $table      = 'product';
    protected $primaryKey = 'pid';

    // protected $returnType    = 'array';
    // protected $useSoftDeletes = true;

    protected $allowedFields = ['pname', 'psku', 'pimage', 'pweight', 'pdescription', 'pquantity'];

    // protected $useTimestamps = false;
    // protected $createdField  = 'created_at';
    // protected $updatedField  = 'updated_at';
    // protected $deletedField  = 'deleted_at';

    // protected $validationRules    = [];
    // protected $validationMessages = [];
    // protected $skipValidation    = false;
}

controller: Product.php

Code:
<?php namespace App\Controllers;
use App\Models\Product;

class Product extends BaseController
{
public function index()
{
$model = new Product();
$data['posts'] = $model->findAll();
return view('home',$data);
}

//--------------------------------------------------------------------
}


view: home.php

Code:
<?= $this->extend('layout/main')?>

<?= $this->section('content')?>

<!-- Caroisel -->
<div id="carouselExampleIndicators" class="row carousel slide" data-ride="carousel">
  <ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner">
<div class="carousel-item active">
  <img class="d-block w-100" src="images/J&U tea.png" alt="First slide">
</div>
<div class="carousel-item">
  <img class="d-block w-100" src="images/J&U tea.png" alt="Second slide">
</div>
<div class="carousel-item">
  <img class="d-block w-100" src="images/J&U tea.png" alt="Third slide">
</div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
  </a>
</div>
<div class="row">
<img src="images/J&U-brand-identity.png" class="img-fluid" alt="J&U-brand-identity">
</div>
    <h1 class="text-center m-5 p-3">Select your tea below to buy your tea online</h1>
list all tea products below


<?php
foreach($posts as $sd){
?>
<?php echo $sd['pname']; ?> <?php}?>





<div class="container-fluid">
<div class="row">
<div class="col-md-6 d-flex justify-content-center">
<video width="600" height="240" controls>
  <source src="images/About J&U Tea.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>
</div>
<div class="col-md-6 text-center">
<h4>Our Belief</h4>
<p>Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. </p>
<a class="btn btn-primary" href="#" role="button">Link</a>
</div>
</div>
</div>
<!-- Lastest 3 blog posts -->
<p class="text-center mt-3 pt-3">Our tea blog</p>
<div class="card-deck pr-5 pl-5">
  <div class="card">
<div class="card-body">
  <h5 class="card-title">Card title</h5>
  <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
  <a href="#" class="btn btn-primary">Read More</a>
</div>
  </div>
  <div class="card">
<div class="card-body">
  <h5 class="card-title">Card title</h5>
  <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
  <a href="#" class="btn btn-primary">Read More</a>
</div>
  </div>
  <div class="card">
<div class="card-body">
  <h5 class="card-title">Card title</h5>
  <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p>
  <a href="#" class="btn btn-primary">Read More</a>
</div>
  </div>
</div>
<!-- About J&U tea -->
<div>
<div class="text-center mt-5">
<p style="font-size: 2.5em;">Why J&U tea?</p>
<p class="text-muted">Premium Quality, Better taste, Mostly straight from garden, Healthy range & Your money for better tea</p>
</div>
<div class="row text-center p-3">
<div class="col-md-3">
<p style="font-size: 1.5em;">Better Quality<p/>
<p class="text-muted">Our tea tasted by professionals and mostly sourced directly from the tea estate</p>
</div>
<div class="col-md-3">
<p style="font-size: 1.5em;">Healthy<p/>
<p class="text-muted">Majority of or range support for healthy living by not adding articial sweetness.</p>
</div>
<div class="col-md-3">
<p style="font-size: 1.5em;">Tradtional Loose Leaf<p/>
<p class="text-muted">We dont have convient tea bag but we value families life where drinking tea from a pot</p>
</div>
<div class="col-md-3">
<p style="font-size: 1.5em;">More value for money<p/>
<p class="text-muted">Our tea mostly less priced than other tea shoppers and we contributed to less fortunate children education</p>
</div>
</div>
<div class="row bg-primary text-white  text-center">
<div class="col-md-4">
<p class="pt-3" style="font-size: 2.5em;">ORDERED SHIPPED</p> <P>WITHIN 3-4 DAYS</P>
</div>
<div class="col-md-4">
<P class="pt-3" style="font-size: 2.5em;">FREE SHIPPING</P> <P> FOR ORDERS OVER AUD 70 </P>
</div>
<div class="col-md-4">
<P class="pt-3" style="font-size: 2.5em;">FRESHNES</P> <P>GUARENTEED </P>
</div>
</div>
<div class="card">
  <div class="card-body text-center">
<p style="font-size: 1.5em;">Do you want to know more about us?</p>
<p>About us, our tea quality, why J&U and many more</p>
<a href="#" class="btn btn-primary">About us</a>
  </div>
</div>
</div>

<?= $this->endSection() ?>

how can i stop this error?
Reply

#5
Please use code tags when submitting php code ie; [ code] your code here [ /code] remove the space in the tags.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
(07-08-2020, 03:19 AM)InsiteFX Wrote: Please use code tags when submitting php code ie; [ code] your code here [ /code] remove the space in the tags.

thank you 
will do
can you help me with retrieving data and display?
Reply

#7
Based on the code provided the only way it can be undefined are this:

Code:
$data = array();
$data['posts'] = $model->findAll();
return view('home',$data);

Other than that you aren't calling it from somewhere.
Reply

#8
(07-08-2020, 05:19 AM)jreklund Wrote: Based on the code provided the only way it can be undefined are this:

Code:
$data = array();
$data['posts'] = $model->findAll();
return view('home',$data);

Other than that you aren't calling it from somewhere.

Thank you so much. will try
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.