0

I have made a mysql query, in to a php file and I want to do same thing but using JDatabase. Let's say for example, we have a table with two columns, column name and column image. Those two columns, contains ten rows. I want to make a list in my php code, that will render something like this:

Name | Image        |
---------------------
AAAA | blahblah.png |
---------------------
BBBB | hahahaha.png |
---------------------

I have made it work using mysql but for some reasons, I should make it using JDatabase. Take an example on what I have now:

This is my query:

$query="SELECT m.name, l.image
          FROM table1 AS m, table2 AS l
         WHERE m.id BETWEEN 70 AND 80
           AND l.id = 10
         ORDER BY m.id ASC";

$result=mysql_query($query);
$num=mysql_numrows($result);

This is my php code:

<?php $count = 0; while ($count < $num){
      $image = mysql_result($result,$count,"image"); ?>
<div><img src="'.$image.'"/></div><?php $count++;}?>

How can I do the same thing using JDatabase and not mysql?

Techie
  • 44,706
  • 42
  • 157
  • 243
DNA180
  • 266
  • 1
  • 7
  • 28
  • 2
    Why you closed this question? Why this question is off topic? Did I ask about cooking or something? Isn't it relative to programming? Some guys in here are very "funny"!!! – DNA180 Jan 18 '13 at 08:59

2 Answers2

1

You can try this-

$db =& JFactory::getDBO();        
$query = "SELECT beta
            FROM table
           WHERE alpha BETWEEN 70 AND 80
           ORDER BY alpha ASC LIMIT 4 , 1";

$db->setQuery($query);
$beta = $db->loadResult();
Irfan
  • 7,029
  • 3
  • 42
  • 57
  • And if I want query have no limits and get value after, using something like this ...`$beta = $db->loadResult($i)`? Thank you for your answer! – DNA180 Jan 14 '13 at 16:56
  • @DNA180: you can use loadObjectList or loadAssocList for more than one result. OR Read here http://docs.joomla.org/Accessing_the_database_using_JDatabase/1.5 – Irfan Jan 14 '13 at 17:06
  • Well I found that. If I do something like this: `$rows = $db->loadObjectList(); $itemrow = $rows[0]; $username = $itemrow->username;` gives me the value of column username only from first row. What is the syntax if I want in `$itemrow` all usernames of all rows? Thank you for your time!!! – DNA180 Jan 14 '13 at 17:54
  • @DNA180: You can try loadResultArray() – Irfan Jan 14 '13 at 18:27
  • 1
    My friend I just edit the question and made it more detailed take a look if you have the kindness!!! Thank you!!! – DNA180 Jan 14 '13 at 18:43
1

Here's another example:

<?php
$db =& JFactory::getDBO();        
$query="SELECT m.name, l.image
          FROM table1 AS m, table2 AS l
         WHERE m.id BETWEEN 70 AND 80
           AND l.id = 10
         ORDER BY m.id ASC";
$db->setQuery( $query );
$rows = $db->loadObjectList();
foreach( $rows as $row ): ?>
    <div><img src="<?php echo $row->image ?>"/></div>
<?php endforeach ?>

EDIT: To access these rows from outside of the loop you can do so by:

$image_1st = $rows[0];
$image_2nd = $rows[1];
$image_3rd = $rows[2];

...and so on

$image_1st_src = $image_1st->image;
$image_1st_src = $rows[0]->image;
WooDzu
  • 4,771
  • 6
  • 31
  • 61
  • 1
    One more question... What if I want to sent this result to unique variables??? I mean, let's say we have 9 rows and I want to do something like this: `$image_1 = 1st_row->image; $image_2 = 2nd_row->image; $image_3 = 3rd_row->image; $image_4 = 4th_row->image; $image_5 = 5th_row->image; $image_6 = 6th_row->image; $image_7 = 7th_row->image; $image_8 = 8th_row->image; $image_9 = 9th_row->image;` – DNA180 Jan 15 '13 at 08:31
  • 1
    I love you man!!! Hahahahaha... Thank you very much!!! :) – DNA180 Jan 15 '13 at 09:53
  • I did up-vote some of your answers around stackoverflow to thank you!!! Shhhh... – DNA180 Jan 15 '13 at 09:58