Welcome Guest, Not a member yet? Register   Sign In
Newbie using helper file
#1

[eluser]quibstar[/eluser]
I'm having an issue using a helper file to get tags for a blog I built. I got it to return the number of tags, but can't seem to work out the code to show an array. I'm using this as kind of a nested query to get all blog posts, comment number and tag associated to post. I use a join table because I want to tally up the number all the different tag. Blog post have many tags and tags have many blog posts.

Here is the code:

<?php
function tags($blog_id)
{
$q=@mysql_query("SELECT * FROM blogtagjoin inner join tags USING(tag_id) WHERE blogtagjoin.blog_id=$blog_id");
$tag=mysql_num_rows($q);
echo $tag;
}
?>

This just breaks everything:
<?php
function tags($blog_id)
{
$q=@mysql_query("SELECT * FROM blogtagjoin inner join tags USING(tag_id) WHERE blogtagjoin.blog_id=$blog_id");
$tag = $this->db->query($q);
foreach ($tag->result_array() as $row)
{
echo $row['tag'];
}
}
?>
any help would be greatly appreciated.
#2

[eluser]bretticus[/eluser]
2 things right off the bat:

1. Never use @ to hide errors. You NEED to see errors. You are developing!

2. CI has a database abstraction layer that you cannot possibly call using PHP's built-in mysql functions.

See some examples here:

I would also recommend that what you are trying to accomplish may be better served with a library than a helper. Especially if you end up with a collection of tags functions (unless that is the only function you will ever write for tags.)

Finally, you can use the database connection CI already instantiates for you (or you do so from your controllers manually.)

See the section called "Utilizing CodeIgniter Resources within Your Library" under the link to libraries above.
#3

[eluser]quibstar[/eluser]
Thanks for the quick response. I actually put this in to the view and it worked.
Code:
$query = $this->db->query('SELECT * FROM blogtagjoin inner join tags USING(tag_id) WHERE blogtagjoin.blog_id=' . $row->blog_id);

        foreach ($query->result_array() as $row)
        {
            echo '<a >'.$row['tag'].'</a> ';
        }
        ?&gt;
I was trying to separate the logic and presentation, but this is alright for now until I get better. by the way this is just a project to learn CI.
#4

[eluser]bretticus[/eluser]
[quote author="quibstar" date="1248799517"]Thanks for the quick response. I actually put this in to the view and it worked.
Code:
$query = $this->db->query('SELECT * FROM blogtagjoin inner join tags USING(tag_id) WHERE blogtagjoin.blog_id=' . $row->blog_id);

        foreach ($query->result_array() as $row)
        {
            echo '<a >'.$row['tag'].'</a> ';
        }
        ?&gt;
I was trying to separate the logic and presentation, but this is alright for now until I get better. by the way this is just a project to learn CI.[/quote]

That's great. Learning CI is definitely worthwhile.

You are right in your assumption that the model (not as in "Model" in MVC) is to separate your layout from your business logic. It's actually easier than you think. All you need to do is pass the $query variable to your view in the controller that loads it. Ex.

your_controller.php:

Code:
&lt;?php
$query = $this->db->query('SELECT * FROM blogtagjoin inner join tags USING(tag_id) WHERE blogtagjoin.blog_id=' . $row->blog_id);
$data['query'] = $query;
$this>load->view('your_view', $data);
?&gt;

your_view.php:

Code:
&lt;html&gt;
&lt;body&gt;
&lt;?foreach ($query->result() as $row):?&gt;
<a>&lt;?=$row->Tag?&gt;</a>
&lt;?endforeach?&gt;
&lt;/body&gt;
&lt;/html&gt;
#5

[eluser]quibstar[/eluser]
I think we're getting closer. I input the information you posted into my controller and view and now I have.

Severity: Notice
Message: Undefined variable: query
Filename: views/blog_view.php
Line Number: 81

Here I my new Controller:
Code:
function blog(){
        $this->load->model('blog_model');
        $this->load->helper('comment_helper');
        $data['records'] = $this->blog_model->getall();
        $this->load->view('blog_view',$data);
        $query = $this->db->query('SELECT * FROM blogtagjoin inner join tags USING(tag_id) WHERE blogtagjoin.blog_id=' . $row->blog_id);
        $data['query'] = $query;
        $this->load->view('blog_view', $data);

    }

and my new view:
Code:
&lt;?php foreach ($query->result() as $row):?&gt;
            &lt;?php echo $row->tag?&gt;
        &lt;?php endforeach?&gt;

Again, thanks for the quick response. Really liking CI.
#6

[eluser]bretticus[/eluser]
You need to call the blog_view view AFTER you set the query data array element. You are calling load twice.
#7

[eluser]quibstar[/eluser]
If I remove the first "$this->load->view('blog_view',$data);" I get these errors.
A PHP Error was encountered

Severity: Notice
Message: Undefined variable: row
Filename: controllers/site.php
Line Number: 39
A PHP Error was encountered

Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/site.php
Line Number: 39

A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
SELECT * FROM blogtagjoin inner join tags USING(tag_id) WHERE blogtagjoin.blog_id=

The "$row->blog_id" is not being passed to the second query.
#8

[eluser]bretticus[/eluser]
[quote author="quibstar" date="1248947828"]If I remove the first "$this->load->view('blog_view',$data);" I get these errors.
A PHP Error was encountered

Severity: Notice
Message: Undefined variable: row
Filename: controllers/site.php
Line Number: 39
A PHP Error was encountered

Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/site.php
Line Number: 39

A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
SELECT * FROM blogtagjoin inner join tags USING(tag_id) WHERE blogtagjoin.blog_id=

The "$row->blog_id" is not being passed to the second query.[/quote]

Is row a variable in your view that is set via $record? You are passing $record via $data['record'] still right?
#9

[eluser]quibstar[/eluser]
No I took it out because you said:
Quote:You need to call the blog_view view AFTER you set the query data array element. You are calling load twice.

I guess I don't understand that statement.
#10

[eluser]bretticus[/eluser]
$data is an array that you are adding variables to that you will eventually pass to your view via:

Code:
$this->load->view('blog_view', $data);

You only need to load your view once after you have compiled all the data to send to it.

CI automatically converts the associative keys under $data into real variables that your view can use. If try and use $records in your view and it has not been set as an associative key ($data['record']) in your controller, you will get "undefined variable" notices.

Again, you can add virtually unlimited variables to $data (or whatever you want to call the array that you will pass to the view loader) before you pass it to the view.

In your original controller code, just remove the first view load.

Code:
function blog(){
        $this->load->model('blog_model');
        $this->load->helper('comment_helper');
        $data['records'] = $this->blog_model->getall();        
        $query = $this->db->query('SELECT * FROM blogtagjoin inner join tags USING(tag_id) WHERE blogtagjoin.blog_id=' . $row->blog_id);
        $data['query'] = $query;
        $this->load->view('blog_view', $data);

    }

Now your view will have access to $records and $query variables at the same time.




Theme © iAndrew 2016 - Forum software by © MyBB