Jump to content


Photo

Wordpress And Database


  • Please log in to reply
2 replies to this topic

#1 cerberus478

cerberus478

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts

Posted 21 November 2012 - 08:10 AM

Hi

I'm using wordpress 3.4.2

Is there a way to insert images into a certain table through wordpress with out using the media library and is there a way to select images and put them on a page.

for example I have a table named flowers and a table named herbs and I want to be able to put all the flowers on the flowers page and all the herbs on the herbs page.

I know that in wordpress you can use
<?php $result = $wpdb->get_results( $wpdb->prepare(
  "SELECT * FROM flowers ORDER BY name",
  $pattern) );
?>

to call all the flowers but I don't know where I would put it.

#2 rjmcintyre7

rjmcintyre7

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 21 November 2012 - 12:18 PM

I'm assuming you mean that you are storing the "paths" to the image files in the database. Your images are stored in your file structure and you want to utilize the database to store and recall the path to those images? If that is the case, the $result will get EVERYTHING from that table. You may want to replace the "*" with the field holding the flower or herb name unless you are trying to really pull all of the data from the table.

I would then check your array after you run the above and
var_dump($result);
to see what is coming out of $result. I assume WP is returning the actual data that was queried, but it's good to be sure. After you var_dump() the data, you should be able to see how to access each element and how each is referenced.

Then, it's time to run through a loop like this (this next example assumes that your array has elements [0][1]...[n] and isn't multi-dimensional:

$output = '
<table>';

//setup foreach loop where $k is the key of each array element of $result and $v is the value it contains (the herb or flower name). This table will ONLY output the name of the flower or herb...no other data. This assumes that your first array element is $result[0] as mentioned above.
foreach($result as $k => $v){
$output.='
	 <tr>
		 <td>'.$v.'</td>
	 </tr>';
}

$output .='</table>';

echo $output;

Let me know if you have questions.

Thanks,
Ryan

Edited by rjmcintyre7, 21 November 2012 - 12:31 PM.


#3 cerberus478

cerberus478

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts

Posted 22 November 2012 - 07:52 AM

I have managed to select my data with a loop and now the only problem I have is that my image doesn't want to work.
I only get a little box with a torn edge.

This is my code
<?php global $wpdb;
$flowers = $wpdb->get_results("SELECT * FROM flowers;");
?>
	
<table>
<?php foreach($flowers as $flower): ?>
<td><?php echo $flower->title ?><br />
<img src="<?php echo $flower->filename; ?>" /><br />
<?php echo $flower->content ?><br />

<?php endforeach; ?>
</td>
</table>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com