korndevil666 Posted October 30, 2008 Share Posted October 30, 2008 Hi, i have two files. client.php/server.php. Basically the client selects a drop down box of the number of rows and number of columns. This is then converted into 1 string and sent to the server. The server then splits the strings back again and then generates the table using the numbers from the strings. What i need help is, basically i've done everything except the loop for actually generating the table using the numbers; because i have no idea. ill post the code below. Its fairly straight forward since im just starting to use php and it isn't particulary good code, but i've commented everything anyway. If anyone could take a look and help me out, much apprechiated! Client.php <html> <head> </head> <body> <?php if(isset($_POST['submit'])) { $row = $_POST["rows"]; $column = $_POST["column"]; $com = ","; $str = ($row . $com . $column); do_it($str); } ?> <form action="<?=$_SERVER['PHP_SELF'];?>" method="post"> <label><strong>Table Generator</strong><br> <br> Rows <select name="rows" id="rows"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> </select> </label> <br> <label> Columns <select name="column" id="column"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> </select> </label> <p> <input type="submit" name="submit" value="Generate"> </form> <? function do_it($str) { // set some variables $host = "localhost"; $port = 10010; $ret; // create socket $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); // connect socket $ret = socket_connect($socket, $host, $port); $str = $str . "\n"; // socket write socket_write($socket, $str); $input = socket_read($socket,1024); $input = trim($input); echo $input; socket_close($socket); } ?> </body </html> Server.php <? // set some variables $host = "localhost"; $port = 10010; // timeout! set_time_limit(30); do{ // create socket $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); // bind socket to port $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); // start listening for connections $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); // accept incoming connections // spawn another socket to handle communication $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); // read client input $input = socket_read($spawn, 1024); // clean up input string $input = trim($input); // set input back to $str $str = $input; //split the text using , to get rows and columns $pieces = explode(",", $str); //set row and column back to variables $rows = $pieces[0]; $column = $pieces[1]; // output the variables $output = ("$rows,$column"); // write back to socket socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); // close sockets socket_close($spawn); socket_close($socket); }while ($input != “END”); ?> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/130777-solved-clientserver-loop-for-generating-a-table-help/ Share on other sites More sharing options...
bobbinsbro Posted October 30, 2008 Share Posted October 30, 2008 i think this should work: <?php $output = "<table>"; for ($i = 0; $i < $rows; ++$i){ $output .= "<tr>"; for($j = 0; $j < $columns; ++$j){ $output .= "<td></td>"; //i'm sure you would like to populate the table cells with something... } $output .= "</tr>"; } $output .= "</table>"; ?> place it before the //write back to socket i didn't populate the table cells with anything, but i put a comment where you should. you can alter anything within the quotes without worry (like adding attributes to the table). Quote Link to comment https://forums.phpfreaks.com/topic/130777-solved-clientserver-loop-for-generating-a-table-help/#findComment-678741 Share on other sites More sharing options...
korndevil666 Posted October 30, 2008 Author Share Posted October 30, 2008 It works - sort of. it doesnt seem to be generating the <td>, just the <tr> output for 5,5 is <table><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr></table> Quote Link to comment https://forums.phpfreaks.com/topic/130777-solved-clientserver-loop-for-generating-a-table-help/#findComment-678747 Share on other sites More sharing options...
korndevil666 Posted October 30, 2008 Author Share Posted October 30, 2008 actually nevermind, it was just a typo on column(s) THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/130777-solved-clientserver-loop-for-generating-a-table-help/#findComment-678751 Share on other sites More sharing options...
bobbinsbro Posted October 30, 2008 Share Posted October 30, 2008 no problem. please set topic to solved. Quote Link to comment https://forums.phpfreaks.com/topic/130777-solved-clientserver-loop-for-generating-a-table-help/#findComment-678754 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.