Jump to content

function data repeat


akurtula

Recommended Posts

i have not been able to figure this out since i have started looking at oo programing:

 

function dis_books($key){
while ($row = mysql_fetch_array($this->result,MYSQL_ASSOC))
   {
   return  $row[$key];
   } 
}
}

 

in the database, i have 5 items; with the above code, i only get the first record:

 

also if i echo rather than return the above, it works ( by showing all records) but then i cant reuse it again e.g

 


   <?php     $con->dis_books(images); ?>



<?php     $con->dis_books(books); ?>




 

is there something that i am missing ?

 

thanks for the help

Link to comment
Share on other sites

I'm not 100% sure I understand what you are attempting to do... Try this though, and see if it does what you want? Basically, the important change is that I have created an array that gets added to during the while loop, rather than just returning the first item in the array. Once the while loop is finished, then the array is returned.

 

function dis_books($key){
while ($row = mysql_fetch_array($this->result,MYSQL_ASSOC))
   {
   $row[] = $row[$key];
   } 
}
return $row[];
}

Link to comment
Share on other sites

thanks for the better explenatio, that's exactly what i need to got - the full array.

 

the code you give looks good though i am getting this error

 

fatal error: cannot use[] for reading in c:\xampp....

 

how can this be solved?

 

thanks for the help

Link to comment
Share on other sites

OK, further tweaks... and I've actually tested this, and it has worked for me on a temporary database.

 

function dis_books($key){
   while ($row = mysql_fetch_array($this->result,MYSQL_ASSOC))
   {
       $array[] = $row[$key];
   } 
   return $array;
}

 

If you are still having trouble after this, perhaps you can post any errors (though there shouldn't be?) or what the function is returning for you?

Link to comment
Share on other sites

hi

 

the above code only gives me the word "array" on the page, therefore no error message

 

though, as it is an array, i tried to then print it using "print_r" just to see what is doing, and it does show that it is holding the records

 

Array ( [0] => Build Your Own Website The Right Way Using HTML & CSS [1] => HTML Utopia: Designing Without Tables Using CSS [2] => Simply JavaScript [3] => Build Your Own Database Driven Website Using PHP and MySQL [4] => No Nonsense XML Web Development With PHP )

 

apart from the 4th entry which is not part of my items - now this is getting strange (the array is getting strange)

 

 

thanks

Link to comment
Share on other sites

Well, you can't simply show array like this: echo $con->dis_books(title); Because it is an array, you'll need to loop through and echo each item separately.

 

As far as the "4th entry not being part of your items", you'd better look at your database. I'm quite sure there isn't an issue with the code... it is only pulling the results you specify.

Link to comment
Share on other sites

thanks for the help

 

a have done this kind of thing before but using the procedural coding - and this was my first time using the class and i got so mixed up - i got so confused and annoyed with this that i did not realize that the 4th item has part of my database record :)

 

but once again thanks alot

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...