Welcome Guest, Not a member yet? Register   Sign In
How can i make model query based on images
#1

(This post was last modified: 08-18-2015, 10:27 PM by freddy.)

Hi thanks for some respon in past now i have been working on my cms and need help

my problem is here tell by images  here http://prntscr.com/85zesi


if in php native i can do that by save each id in every function but in model codeigniter i'm still figure it out !

and here is my model


Code:
//detail trend

function detailtrend($art_slug)     //consider art_slud is the max id from table    
    {
        return $this->db->query("SELECT * FROM `articles` WHERE art_slug='$art_slug'");
    }

//sisa trend
function lefttrend($art_slug)        
    {
        return $this->db->query("SELECT * FROM `articles` WHERE art_slug != '$art_slug' and art_jenis='TREND' limit 1");
    }

//how can i write funtion as your said above

I don't know how to get data in the images, if any some suggest feel free to comment here, thanks
Reply
#2

(This post was last modified: 08-18-2015, 11:15 PM by Wouter60.)

In CI it is much easier.

PHP Code:
$query=$this->db->query("SELECT * FROM table ORDER BY id DESC LIMIT 4"); 
To display the first row (your latest article):
PHP Code:
$main_article $query->row(0); 

To display the next article at the top your right column:
PHP Code:
$topright_article $query->row(1); 
To display the next 2 items:
PHP Code:
for ($x=3$x<5$x++) {
  $right_article[] = $query->row($x);


If you want to do this in a model, make your model return the $query object.
Reply
#3

(This post was last modified: 08-19-2015, 03:50 AM by freddy.)

Oh great thanks for comment, hem it's easy things after read the comments, thanks bro, how can i +1 or solved this thread Big Grin

edit 


How the last wuery show 2 last from 4 data itself. don't know how to figure it out so isu this twice here is my controller

Code:
$data['leftarticle'] = $this->model_front->lefttrend();

then here is view i use twice 
Code:
<!---articles 1 main-child--->
<?php
$row2 = $leftarticle->row_array(2);
$title2=$row2['art_title'];
$art_image2=$row2['art_image'];
$art_slug2=$row2['art_slug'];
?>

<div class="col-lg-12 child-articles-trend ">
    <img  src="<?php echo base_url() ?>uploads/article/<?php echo $art_image2 ?>" alt="<?php echo $title2 ?>">
<a href="<?php echo base_url()?>trend-article/<?php echo $art_slug2  ?>" ><h4><?php echo $title2 ?></h4>
</a>
</div>

and this show another view
Code:
<!---articles 1 main-child--->
<?php
$row3 = $leftarticle->row_array(3);
$title3=$row3['art_title'];
$art_image3=$row3['art_image'];
$art_slug3=$row3['art_slug'];
?>

<div class="col-lg-12 child-articles-trend">
    <img  src="<?php echo base_url() ?>uploads/article/<?php echo $art_image3 ?>" alt="<?php echo $title3 ?>">
        <a href="<?php echo base_url()?>trend-article/<?php echo $art_slug3  ?>" ><h4><?php echo $title3 ?></h4>
        </a>
</div>



It give me redudancy, how can i make right query based on your suggest in the last query, thanks
Reply
#4

This just requires a little prep-work in the controller to get the data into the form that makes it easiest to work with in the view:
PHP Code:
$leftarticle $this->model_front->lefttrend();
$rows = array();
$rows[] = $leftarticle->row_array(2);
$rows[] = $leftarticle->row_array(3);

// Alternatively, you could add a method to your model (or change the existing one)
// to only return the rows you need in the first place.

$data['rows'] = $rows

Then, in your view, you can loop through the array instead of creating multiple views:
PHP Code:
<?php foreach ($rows as $row) : ?>
<div class="col-lg-12 child-articles-trend">
    <img src='<?php echo base_url("uploads/article/{$row['art_image']}"); ?>' alt="<?php echo $row['art_title']; ?>" />
    <h4><a href='<?php echo base_url("trend-article/{$row['art_slug']}"); ?>'><?php echo $row['art_title']; ?></a></h4>
</div>
<?php endforeach; ?>

Or, if you don't want to use a foreach() inside your view, you can put a foreach() loop inside your controller and load the same view for each row:
PHP Code:
// $data['rows'] = $rows;

foreach ($rows as $row) {
    $data['row'] = $row;
    $this->load->view('your_view'$data);


Then 'your_view' would just be something like this:
PHP Code:
<div class="col-lg-12 child-articles-trend">
 
   <img src='<?php echo base_url("uploads/article/{$row['art_image']}"); ?>' alt="<?php echo $row['art_title']; ?>" />
 
   <h4><a href='<?php echo base_url("trend-article/{$row['art_slug']}"); ?>'><?php echo $row['art_title']; ?></a></h4>
</div> 
Reply
#5

(This post was last modified: 08-19-2015, 08:44 PM by freddy.)

Thanks mwhitney but has bugs in here 
Code:
//$query=$this->db->query("SELECT * FROM `articles` WHERE art_jenis='TREND' ORDER BY art_id DESC limit 4");
        //$data['main'] = $query->row(3);
        $leftarticle = $this->model_front->lefttrend();
        $rows = array();
        $rows[] = $leftarticle->row_array(2);
        $rows[] = $leftarticle->row_array(3);
        $data['rows'] = $rows;
        $this->load->view('row',$data);
    }

when data more than 4 or even just 4 it's oke no problem with view but i'm trying to delete 1 so data 4-1=3 it show twice here is images http://prntscr.com/86p3ga

then i'm trying to delete again to  be 2 data (3-1=2)  in table it also show it, it suppose to be only show 2 row without third row, i mean 2 row show twice too.

then tested remove data and remain 1 data also show twice, what do i wrong here ?

Please Correct me where i should give if, Thanks 
Reply
#6

Use $query->num_rows() to determine how many records have been found in the database.
Reply
#7

(08-19-2015, 09:47 PM)Wouter60 Wrote: Use $query->num_rows() to determine how many records have been found in the database.

Did you mean put it in model like this
Code:
$query=$this->db->query("SELECT * FROM `articles` WHERE art_jenis='TREND' ORDER BY art_id DESC limit 4")->num_rows;

then how in view to use if data only 1 or 2 to make my data not redudancy, thanks wouter60 for comments
Reply
#8

PHP Code:
$query $this->db->query("SELECT * FROM `articles` WHERE art_jenis = 'TREND' ORDER BY art_id DESC limit 1,2"); 

I've changed the limit portion of the query here to return 2 items, skipping the first item (if I wanted the first 2 items, I could use "limit 0,2" or "limit 2").

Then, instead of using the row_array() function, you can use result_array():
PHP Code:
$rows $query->result_array();
$data['rows'] = $rows

Personally, I would have my model return $query->result_array() or $query->result() rather than returning $query, but that's up to you.

If you're getting duplicates when doing it this way, you should probably check your data.
Reply
#9

(This post was last modified: 08-21-2015, 03:22 AM by freddy.)

(08-20-2015, 07:33 AM)mwhitney Wrote:
PHP Code:
$query $this->db->query("SELECT * FROM `articles` WHERE art_jenis = 'TREND' ORDER BY art_id DESC limit 1,2"); 

I've changed the limit portion of the query here to return 2 items, skipping the first item (if I wanted the first 2 items, I could use "limit 0,2" or "limit 2").

Then, instead of using the row_array() function, you can use result_array():


PHP Code:
$rows $query->result_array();
$data['rows'] = $rows

Personally, I would have my model return $query->result_array() or $query->result() rather than returning $query, but that's up to you.

If you're getting duplicates when doing it this way, you should probably check your data.

Sorry late respons cause i'm doing responsive design  Confused by the way why you change query becom limit 2 mwhitney  all i want is take  4 data then my problem now is show the third and also 4 in other hand i have been tested it delete one when data still 4 it give me duplicate data which should only show data with row 3 then i delete again till data 2 it also duplicate, will you give me example how to show it with right ways


Thanks for respond ya 
Reply
#10

(08-21-2015, 03:16 AM)freddy Wrote: by the way why you change query becom limit 2 mwhitney  all i want is take  4 data then my problem now is show the third and also 4 in other hand i have been tested it delete one when data still 4 it give me duplicate data which should only show data with row 3 then i delete again till data 2 it also duplicate, will you give me example how to show it with right ways

All I did was change the query to reflect what you were doing in the code by taking row_array(2) and row_array(3).

I think that we're running into a few different problems here simply because we're working off small snippets of code instead of the relevant controller, model, and view(s) and also dealing with a bit of a language barrier.

To go back to the original function you supplied:
PHP Code:
function lefttrend($art_slug)        

{
    return $this->db->query("SELECT * FROM `articles` WHERE art_slug != '$art_slug' and art_jenis='TREND' limit 1")


This could be modified as follows (using query builder because it makes more sense to me than manually editing a query string, especially with MySQL's syntax for this):
PHP Code:
function lefttrend($art_slug$limit 0$offset 0)        

{
    if ($limit 0) {
        if ($offset 0) {
            $this->db->limit($limit$offset);
        } else {
            $this->db->limit($limit);
        }
    }
    $this->db->where(array('art_slug !=' => $art_slug'art_jenis' => 'TREND'));

    return $this->db->get('articles');


Then you can get whatever range of values you need from the query by passing in the relevant $limit and $offset, or you can retrieve the full query results by leaving them out. So, you could get the first one by calling
PHP Code:
$first lefttrend($art_slug1
or get just the second and third by calling
PHP Code:
$twoThree lefttrend($art_slug21); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB