hoponhiggo Posted July 19, 2011 Share Posted July 19, 2011 Hi I am learning both PHP an HTML and was wondering if i could ammend my code to display my 'member cards' in a table - two columns, unlimited rows? The code in question is: { echo "<div id='memcontainer'> <div id='mempic'>$img</div> <div id='memname'><a href='members.php?user=$user[username]'>$user[username]</a></div></div><br> "; } each record is contained in the 'memcontainer' div Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/ Share on other sites More sharing options...
Maq Posted July 19, 2011 Share Posted July 19, 2011 Sure, just adjust the HTML in your echo by following this tutorial: http://www.quackit.com/html/html_table_tutorial.cfm Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1244792 Share on other sites More sharing options...
hoponhiggo Posted July 19, 2011 Author Share Posted July 19, 2011 i understand that i can add the <table></table> tags, but im not sure how i can set the amount of columns to 2? Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1244801 Share on other sites More sharing options...
Maq Posted July 19, 2011 Share Posted July 19, 2011 i understand that i can add the tags, but im not sure how i can set the amount of columns to 2? Look at the first example, it shows you. Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1244804 Share on other sites More sharing options...
hoponhiggo Posted July 20, 2011 Author Share Posted July 20, 2011 I still cant figure this out? Surely this example only works if you have different sets of data for each column? I have the same data, i just want it spread over two columns so they appear side by side on my page? Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1245313 Share on other sites More sharing options...
cssfreakie Posted July 20, 2011 Share Posted July 20, 2011 I recommend the following: 1) Maybe, you should first learn how to make a table in html. (as suggested by Maq) Ones you made a good table that looks nice and is valid. Think about what parts of the table can stay the same. and which ones increase. Like you said you want an 'infinite' amount of rows. (hint hint) 2) Ones you've done that, learn how to use loops in php (there is a nice tutorial here at phpfreaks under tutorials). Pretty soon you will find out which loop is the correct one to use. try it test it and play with it. 3) After that combine the logic of php inside your table. There is no good reason to try and combine them from the start if you can't play with them apart. This way you can make sure your logic works, your html works and all you need to do is combine them. As an example with a an unordered list. I could do it with a table but you wont learn anything from it. <!-- 1) a normal list --> <ul> <li>A</li> <li>B</li> <li>C</li> <li>D</li> </ul> <!-- 2) some php loop --> <?php //first an array $myarray = array('A','B','C','E'); foreach($myarray as $value){ echo $value; } // 3) apply php logic to the html echo '<ul>'; //begin foreach($myarray as $value){ echo '<li>'.$value.'</li>'; } echo '</ul>'; //end Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1245366 Share on other sites More sharing options...
hoponhiggo Posted July 21, 2011 Author Share Posted July 21, 2011 I already have a lot of the php and bits of html & css sorted. See image.... This loops out the records down the page. What i want is for them to be side by side. Im not even sure using a table would be the best way to do this? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1245494 Share on other sites More sharing options...
Maq Posted July 21, 2011 Share Posted July 21, 2011 Yes, for tabular data you should use tables. The format should look like: echo "</pre> <table>"; //while loop { echo ""; echo "$img"; echo "{$user['username']}"; echo ""; } echo "</ta For each iteration create a single row (tr) and 2 columns in each one (td). Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1245684 Share on other sites More sharing options...
Muddy_Funster Posted July 21, 2011 Share Posted July 21, 2011 I think he actualy wants the full contents of <div id='memcontainer'> to apear in two by x format, rather than 1 by x format as opposed to using a table in place of the memcontainer object. On another note - if you are calling the same <div> more than once on a given page, you really should make it a class not an id - id's are designed to be unique and as such should only appear once on a page. Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1245696 Share on other sites More sharing options...
hoponhiggo Posted July 21, 2011 Author Share Posted July 21, 2011 I think he actualy wants the full contents of <div id='memcontainer'> to apear in two by x format, rather than 1 by x format as opposed to using a table in place of the memcontainer object. What muddy said... Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1245714 Share on other sites More sharing options...
wildteen88 Posted July 21, 2011 Share Posted July 21, 2011 Have a look at this post for creating multi-column results. Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1245798 Share on other sites More sharing options...
phpSensei Posted July 21, 2011 Share Posted July 21, 2011 I have a calendar class I can give you which I wrote in PHP, I dont know if its too complex for you to understand but it uses what you want to do, let me know. However just create a counter and for every 3 columns reset it to 0 and so forth. Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1245802 Share on other sites More sharing options...
hoponhiggo Posted July 22, 2011 Author Share Posted July 22, 2011 Thanks for all the help guys. Im gonna have a crack at this and see how i get on. Phpsensei...Yeah that would be great. Any help or reference i can get will be useful. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1246048 Share on other sites More sharing options...
hoponhiggo Posted July 25, 2011 Author Share Posted July 25, 2011 Hi Guys I have tried and tried to make this work with my code, but it just doesnt work. Could somebody please take a look at my code and advise? <table cellspacing="2" cellpadding="2"> <?php //gets all the members from the database $getusers = mysql_query("SELECT * FROM `users` ORDER BY username ASC") or die(mysql_error()); if($getusers && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 2; //loops there name out while ( $user = mysql_fetch_array($getusers) ) { $username = $user['username']; // make the variables easy to deal with extract($row); // open row if counter is zero if($i == 0) if($user != "" && $user != null) echo "<td>$product</td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; //to display image from source $dir = "prof_pics"; $sql = "SELECT prof_pic FROM users WHERE username = '$username'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("Username not found in database."); $row = mysql_fetch_array($res); $pic="$dir/".$row['prof_pic']; $img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>"; { echo "<div id='memcontainer'> <div id='mempic'>$img</div> <div id='memname'><a href='members.php?user=$user[username]'>$user[username]</a></div><br> <div class='addfriend'><a style='text-decoration:none' href='friendrequest.php?user=$user[username]'><font color='red'>Add as Friend</a></div> </div><br> "; } } } } } // clean up table - makes your code valid! if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; } ?> </tr> </table> echo "<center>"; ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/242361-can-these-be-echoed-out-in-a-table/#findComment-1246887 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.