9 Replies - 397 Views - Last Post: 30 November 2012 - 01:08 PM Rate Topic: -----

#1 ats3216  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 15-November 11

Filling a table accessed through a database

Posted 30 November 2012 - 12:27 PM

Hello,

I'm trying to solve a problem where I have a database i am accessing for information to fill out a table on my website. It is a PHP site, and i'm trying to space the data out in a table that has columns that are 33% of the width of the area they are in. For whatever reason, it is not displaying any of the data. The code I currently have is below.

	$count = 0;
	$PageContent .= '<table>';
	$PageContent .= '<tr>';
	$Fields = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."fsrep_fields ORDER BY field_order");
	foreach ($Fields as $Fields) {
		
		$PageContent .= '<p><strong>'.$Fields->field_name.': </strong>'.'</p>';
		
		$HouseFieldInfo = $wpdb->get_var("SELECT listing_value FROM ".$wpdb->prefix."fsrep_listings_to_fields WHERE field_id = $Fields->field_id AND listing_id = $ListingDetails->listing_id");
		
			
		
			
		foreach ($HouseFieldInfo as $HouseFieldInfo)
		{
			$newRow = $count % 3;
			if ($newRow === 0)
			{
				$PageContent .= '</tr>';
				$PageContent .= '<tr>';
			}
			$count = $count+1;
		
		
			$PageContent .= '<td width="33%">';
			if ($HouseFieldInfo != '') {
			$PageContent .= '<p>'.$HouseFieldInfo.'</p>'.'<br>';
				//$HouseFieldInfo
			$PageContent .= '</td>';
			}
			}
		
		
	}
	$PageContent .= '</tr>';
	$PageContent .= '</table>';
	$PageContent .= '<p>&nbsp;</p>';



Any suggestions on how to get this to display? Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Filling a table accessed through a database

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5407
  • View blog
  • Posts: 27,410
  • Joined: 10-May 07

Re: Filling a table accessed through a database

Posted 30 November 2012 - 12:31 PM

Quote

$Fields = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."fsrep_fields ORDER BY field_order");

foreach ($Fields as $Fields) {


Here you assume that $Fields was populated. Was it? Validate your variables, don't just code it to work.
Was This Post Helpful? 0
  • +
  • -

#3 ats3216  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 15-November 11

Re: Filling a table accessed through a database

Posted 30 November 2012 - 12:34 PM

Yes $Fields is populated. I can guarantee that. I have checked and rechecked my database.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5407
  • View blog
  • Posts: 27,410
  • Joined: 10-May 07

Re: Filling a table accessed through a database

Posted 30 November 2012 - 12:36 PM

K, you checked the database. But your code just assumes that $Fields contains a value. Why not check it in your code?

Your question is that you don't know why there isn't any output. Your code isn't written to validate, it was written just assuming values are there. & that's why you don't know why it isn't working. You have no error checking in place.
Was This Post Helpful? 0
  • +
  • -

#5 ats3216  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 15-November 11

Re: Filling a table accessed through a database

Posted 30 November 2012 - 12:39 PM

Oh, I see what your saying. That makes sense. How would I go about error checking on this, just trying to print the value of $Fields each time through the loop?
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5407
  • View blog
  • Posts: 27,410
  • Joined: 10-May 07

Re: Filling a table accessed through a database

Posted 30 November 2012 - 12:41 PM

Here is how I would do it :

$SQL = "SELECT * FROM ".$wpdb->prefix."fsrep_fields ORDER BY field_order";
$Fields = $wpdb->get_results($SQL);
if(!$Fields) {
  echo "The following SQL Failed : <br>".$SQL;
  die();
}
foreach ($Fields as $Fields) {
...


Was This Post Helpful? 1
  • +
  • -

#7 ats3216  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 15-November 11

Re: Filling a table accessed through a database

Posted 30 November 2012 - 12:47 PM

Alright, so I tried running that and it came back with nothing, so I assume it passed the test. Is there anything else you see that could possibly hinder the output? Thanks again for helping. I appreciate it.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5407
  • View blog
  • Posts: 27,410
  • Joined: 10-May 07

Re: Filling a table accessed through a database

Posted 30 November 2012 - 12:51 PM

Did you just update this one sql statement, or did you update them all? Like on line 9....

Code it to check for failure, not to function :)
Was This Post Helpful? 0
  • +
  • -

#9 ats3216  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 15-November 11

Re: Filling a table accessed through a database

Posted 30 November 2012 - 01:04 PM

Oooh, your correct! It looks as though it isn't pulling from my $HouseFieldInfo array for whatever reason. I have a feeling that the database is somehow concatenating everything I put into $HouseFieldInfo, because when I used the test code, it came out with everything in $HouseFieldInfo, not just the first element....

Anyways, thank you for your help! I appreciate it! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5407
  • View blog
  • Posts: 27,410
  • Joined: 10-May 07

Re: Filling a table accessed through a database

Posted 30 November 2012 - 01:08 PM

Glad that got you in the right direction. Hopefully you'll get it figured out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1