ronnievanderstegen Posted February 28, 2013 Share Posted February 28, 2013 Hi I want to make a table that has table rows like this: There are 3 horses in the sky. There are 4 cows in the stable. There are 5 dogs in the sea. The numbers, the animal names and the places have to be placed neatly below each other, so therefore I probably need to use the <td></td>. I tried using the printf ($format, $number, $animal, $place) function, and the array's $number=array(3,4,5); $place=array(sky,stable,sea); $animal=array(horses, cows, dogs); But I only get one tablerow that says (, There are, Array, Array, in the, Array); Can anyone please tell me what code I have to use to make this? Thanks already. Quote Link to comment https://forums.phpfreaks.com/topic/275048-printf-format-in-table/ Share on other sites More sharing options...
timothyarden Posted February 28, 2013 Share Posted February 28, 2013 print_r() is used to print arrays (to structure them add the HTML <pre> </pre> tags) Suggest using a foreach loop and adding the values to a string which you then echo after the loop Quote Link to comment https://forums.phpfreaks.com/topic/275048-printf-format-in-table/#findComment-1415570 Share on other sites More sharing options...
Christian F. Posted February 28, 2013 Share Posted February 28, 2013 (edited) You should be using a foreach on this, yes. However, Timothy didn't make it clear as to how or why. Not sure where he got print_r from though... Use the foreach to loop through one of the arrays, saving both the key and the value. Then use the key to reference the correct value from the other arrays as well. Then you can use sprintf to build the string inside the loop, after which you can echo it out wherever you need it. Edited February 28, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/275048-printf-format-in-table/#findComment-1415574 Share on other sites More sharing options...
ronnievanderstegen Posted February 28, 2013 Author Share Posted February 28, 2013 Thanks already for the answers. I'm relatively new to php, if it isn't to much trouble, could you please post the right code? Quote Link to comment https://forums.phpfreaks.com/topic/275048-printf-format-in-table/#findComment-1415609 Share on other sites More sharing options...
Christian F. Posted February 28, 2013 Share Posted February 28, 2013 Sure, I could do that, if you don't mind paying for my time? Writing code is what I do for a living, after all. My intention for answering people on this forum is to help people learn, not do their work for them for free. I've given you the tools and references you need to (try to) figure this one out yourself. Now, go forth and prove thyself worthy of help! After all, if you can't be bothered to spend the time on it, why should I? Think about that. Quote Link to comment https://forums.phpfreaks.com/topic/275048-printf-format-in-table/#findComment-1415612 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.