Jump to content

[SOLVED] Client/Server - Loop for generating a table help


Recommended Posts

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]

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).

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.