0

I'm quite new to PHP OOP and I have a question regarding a simple MYSQL query. I have an index.php page where I want to output my results from my query.class file.

Here is my method,

 public function Query($rowName) {

   $q = mysql_query("SELECT * from p_tuts");

   while($row = mysql_fetch_array($q)) {

       echo $row[$rowName];
   }
 }

Now this works fine and I can call it though my index.php file

$Database->Query('tut_content');

But my issue is I want to wrap each content block in DIV containers and don't want to have to echo the HTML code in the class file, so I want to really echo the row's data in the index file, but unsure how to do this.

Kind regards

1
  • can you also paste some more code so that someone can suggest to where to put divs and also that could help understanding what exactly you want. Commented Jul 26, 2011 at 15:19

5 Answers 5

2

Pass the rows back as an array.

public function Query($colName) {

    $q = mysql_query("SELECT * from p_tuts");
    $rows = array();
    while($row = mysql_fetch_assoc($q)) {
        $rows[] = $row[$colName];
    }
    return $rows;
}

It's better this way anyway, because it keeps database code away from output code (i.e. echo).

Then output it like so:

<?php
$results = $Database->Query('tut_content');
foreach ($results as $result): ?>
    <div><?php echo $result; ?></div>
<?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

6 Comments

Ah thank you, perfect! :) I think I better read more! that's been driving me crazy for a while.
My answer would have been better served with an example as above :-)
My question now would be though, once you've returned the rows, is there a way to be more specific, for instance return different values $row->title, $row->name, $row->href for instance?
@user863739: You mean pass multiple columns in the rows, instead of just one?
Yes, that's right Jonah. I nearly figured it out <?php foreach ($results as $result): $title = $result['tut_poster']; $href = $resultp['tut_href']; ?> <div style="border:1px solid red"><?php echo $title; ?></div> <?php endforeach; ?> although would I put the title and href variables within my class?
|
0

You dont want to be doing the echo in your class.

You want to return the row...

This way

 $Database->Query('tut_content'); 

Can be wrapped in the DIV / whatever you need...

1 Comment

Yes I know, although the issue is, every row needs to have it's own div class, if I echo it out like that it will all wrap in one div :)
0

OOP functions are the same as normal function. The function as you have it only echos $row[$rowname]. You either want to modify it to have it echo <div>$row[$rowname]</div> or have it return the row values as an array:

while($row = mysql_fetch_array($q)) {
   $output[] = $row[$rowName];
}
return $output;

Comments

0

use the MVC pattern.

http://php-html.net/tutorials/model-view-controller-in-php/

the database belongs to the Model

Good Luck

Comments

0

Or you can use my favorite database layer http://dibiphp.com/. It's small but smart :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.