Jump to content

problem looping through database fields


surreal5335

Recommended Posts

I am trying to loop through a series of database fields and have print out on the web page. My one function that prints out just a specific group of fields works fine:

 

/**
* returns array of a post from database
* @returns array
*/

function find_post($id) {

	$connection = db_connect();
	$query = sprintf("select posts.title, posts.body, posts.user_id, users.name 
		from 
			posts, users 
		where 
			posts.user_id = user_id and posts.id = %s",

			mysql_real_escape_string($id)
			);

	$result = mysql_query($query);

	$number_of_posts = @mysql_num_rows($result);
	if ($number_of_posts == 0) {
		return false;
	}

	$row = mysql_fetch_array($result);

	return $row;
}

 

 

But my function that has problems getting the data from the database is:

 

/**
* returns array of posts from database
* @returns array
*/

function find_posts() {

	$connection = db_connect();
	$query = 'select posts.title, posts.body, posts.user_id, users.name 
		from 
			posts, users 
		where 
			posts.user_id = user_id';

	$result = mysql_query($query);

	$number_of_posts = mysql_num_rows($result);
	if ($number_of_posts == 0) {
		return false;
	}

	$result = result_to_array($result);

	return $result;
}

 

While I am not getting any errors, I am also not getting any data from my database either

 

I have already had a problem with this function about the function it calls within it:

 

	function result_to_array($result) {

	$result_array = array();
	for ($i=0; $row = @mysql_fetch_array($result); $i++) {
		$result_array[$i] = $row;
	}

	return $result_array;
}

 

While I am feeling good about being able to fix a majority of my errors, this one I am not sure about.

 

I appreciate any help in the matter

Link to comment
Share on other sites

Thanks for the help, I can now come to safe conclusion that find_posts() is not the problem.

 

well I spilled over my code for a couple hours and turned on php error reporting.

 

These are the set of notices I am getting:

 

 

Notice: Undefined variable: post in /home1/royalvil/public_html/post_ads/_post.php on line 7

 

Notice: Undefined variable: post in /home1/royalvil/public_html/post_ads/_post.php on line 11

 

Notice: Undefined variable: post in /home1/royalvil/public_html/post_ads/_post.php on line 15

 

 

currently the code for that page is as follows:

 


<div class="post">
<h2><?php echo $post['title']; ?></h2>

<span class="body-text">
<?php echo $post['body']; ?>

</span>
<span class="username">
<?php echo $post['username']; ?>

</span>
<span class="email">
<?php echo $post['email']; ?>

</span>

</div>

 

right now my code for calling the looping function is being assigned to $posts. I have tried many ways to make it work as $post so the code above will get the info from find_posts() properly. When ever I try to change it over I get this notice:

 

Notice: Undefined index: body in /home1/royalvil/public_html/post_ads/_post.php on line 6

 

Notice: Undefined index: username in /home1/royalvil/public_html/post_ads/_post.php on line 10

 

Notice: Undefined index: email in /home1/royalvil/public_html/post_ads/_post.php on line 14

 

the only place I can find reference to $posts in my code is here:

 

//$posts = find_posts();

foreach($posts as $post) {

	echo '<h2>'.$post['title'].'</h2><br/>';
	echo $post['body'].'<br/>';
	echo $post['email'].'<br/>';
	echo $post['name'].'<br/>';
}

 

keep in mind these code snipets are spread out over several pages. Now this may seem like the answer, but I have tried leaving them uncommented so they would active, but that made no difference.

 

Does anyone have any suggestins as to what I can do?

 

Thanks a lot for the help

Link to comment
Share on other sites

Hi

 

Still can't see anything obvious, but it does appear your $post array isn't being fully populated as you expect it to be.

 

I would spread a few echo statements around the functions collecting the data to check exactly what is being brought back.

 

All the best

 

Keith

Link to comment
Share on other sites

well I tried the suggestion you made.

 

When I did that I got this output:

 

Array

 

I also changed a bit of code in find_posts() as someones suggestion

changing:

$result = result_to_array($result);

 

to this:

$row = mysql_fetch_array($result);

 

which gave me an output of:

 

Resource id #7

 

not really sure what to make of this

any thoughts?

Link to comment
Share on other sites

Thanks a lot for the suggestion, I dont know why I didnt think of it before.

 

I used this code:

 

$posts	= find_posts();
print_r($posts);

 

which gave me this output:

 

Array ( [0] => Array ( [0] => painter needed for siding [title] => painter needed for siding [1] => I need a painter to paint the side of my house. pay is negotiable. paid lunches. [body] => I need a painter to paint the side of my house. pay is negotiable. paid lunches. [2] => 1 [user_id] => 1 [3] => super_dude [name] => super_dude ) [1] => Array ( [0] => carpet layer needed [title] => carpet layer needed [1] => I need a carpet layer that has good skills with stairs [body] => I need a carpet layer that has good skills with stairs [2] => 1 [user_id] => 1 [3] => super_dude [name] => super_dude ) )

 

it seems obvious that  the data is beng gathered into the array from the database, but is failing to get echoed in the html layout I created.

 

I believe this problem to be because find_posts() is assigned to $posts. While the data echoed out in the html layout is looking for data in $post not $posts.

 

Which also explains why I am getting this notice from php error reporting:

 

Notice: Undefined variable: post

 

In this url route all the database data is assigned to $posts. My other url route that serves a different function uses $post which works fine with the html layout code. My hope is to be able to assign find_posts() to $post without getting this message from php:

 

Notice: Undefined index: body

 

body is a field name in my database if there was any confusion.

 

Does any one have any suggestions on how to accomplish this?

 

Link to comment
Share on other sites

Hi

 

This is the problem with dealing with small fragments of code.

 

foreach($posts as $post) {

	echo '<h2>'.$post['title'].'</h2><br/>';
	echo $post['body'].'<br/>';
	echo $post['email'].'<br/>';
	echo $post['name'].'<br/>';
}

 

will work fine. It is looping round each occurance of $posts and assigning each in turn to $post. As such that will be fine.

 

However I cannot tell if the other fragment you posted earlier which refers to $post is within a similar loop.

 

All the best

 

Keith

Link to comment
Share on other sites

Thanks for the great suggestion, got me out of the woods and into more comfortable territory.

 

the suggestion yu made corrected my current problem but messed up the other single post that didnt loop using $post as a variable.

 

So I made an if test to check the url:

 

if ($route['view'] == 'index') {
foreach($posts as $post) {
include('_post.php');
}
} else {
include('_post.php');
}

 

_post.php has the html layout in it.

 

Thanks a lot for pointing me in the right direction!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.