Forum Moderators: coopster

Message Too Old, No Replies

php string add data to html table ?

         

mikejs

12:49 am on Oct 29, 2009 (gmt 0)

10+ Year Member



Hi I have a string of data that I can convert to an array, with this data I want to style a table i.e. border size, font colour, backround colour and populate some of the colums including header info ho wwould I go about doing this

lets say the table is fixed 2 * 3 giving me 4 boxes and two headers

Pets ¦ Cars

dog ¦ ford
cat ¦ bmw

so if the array had

[0] Pets
[1] Cars
[2] dog
[3] cat
[4] ford
[5] bmw
[6] red = font colour
[7] 2 = table border
[8] grey = backround color

how would I go about populating the 2 * 3 table and change the tables attributes

thanks in advance

M

daveginorge

11:37 am on Oct 29, 2009 (gmt 0)

10+ Year Member



Hi Mike.

This might not be a solution with finesse but it will do the job. The thing with your data is that it is not following a easy to manage format.
Pet Car Cat Dog Ford BMW it would have been cleaner if the data was
Pet Car Cat Ford Dog BMW.

that said try this


<?php
$my_array = array('Pets', 'Cars', 'dog', 'cat', 'ford', 'bmw', 'red', '2px solid #000', 'grey' );

echo "<table style='color: $my_array[6]; border: $my_array[7]; background-color: $my_array[8]; '>\n";
echo "<tr><td>" . $my_array[0] . "</td><td>" . $my_array[1] . "</td></tr>\n";
$z = 2;
for ($i=0; $i < 2; $i++) {
echo "<tr><td>" . $my_array[$z] . "</td>";
echo "<td>" . $my_array[$z + 2] . "</td></tr>\n";
$z++;
}
echo "</table>\n";

?>


The "\n" just keep the source code easy to read when you view it in the browser.
Hope this helps you get started.
Dave.

mikejs

12:27 pm on Oct 29, 2009 (gmt 0)

10+ Year Member



Hi thanks for your reply I think if I provide the forms I am trying to use its a client server socket program I have a client that will input the data and the fill out the form with the details I have added your table sugestion but it gives an error

<?php
// set some variables
$host = "localhost";
$port = 12849;
// don't timeout!
set_time_limit(0);
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);
//load input into an array
$seperators = array('" "','" ',' "');
$output = str_replace($seperators,'_',$str);//adds _ between strings
$str = explode('_',$str); //seperates the aray
//echo $str; test output corect string_string_ etc
for($i=0;$i<count($ary);$i++) // for loop through the aray
$ary[$i] = str_replace('"','',$ary[$i]);

//echo '<pre>';
//print_r($ary); //prints location and value in the aray
//echo '</pre>';

// reverse client input and send back
$output = "<table style='color: $ary[6]; border: $ary[7]; background-color: $ary[8]; '>\n
<tr><td>" . $my_array[0] . "</td><td>" . $ary[1] . "</td></tr>\n"
$z = 2;
for ($i=0; $i < 2; $i++) {
"<tr><td>" . $ary[$z] . "</td>"
"<td>" . $ary[$z + 2] . "</td></tr>\n"
$z++;
}
"</table>\n";

socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");

// close sockets
socket_close($spawn);
socket_close($socket);
}while ($input != "END");
?>

<?php
if(isset($_POST['submit'])) {

$str = $_POST['fname'].' '.$_POST['fname2'].' '.$_POST['fname3'].' '.$_POST['fname4'].' '.$_POST['fname5'].' '.$_POST['fname6'].' '.$_POST['fname7'].' '.$_POST['fname8'];
//echo $str;
//print_r (explode(" ",$str));//
do_it($str);
}
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p><br>
<input type="text" name="fname" value="" >
<input type="text" name="fname1" value="" >
<input type="text" name="fname2" value="" >
<input type="text" name="fname3" value="" >
<input type="text" name="fname4" value="" >
<input type="text" name="fname5" value="" >
<label>
<br />
<input type="checkbox" name="fname6" value="black">
black</label>
<br>
<label>
<input type="checkbox" name="fname6" value="blue">
Blue</label>
<br>
<label>
<input type="checkbox" name="fname6" value="red">
green</label>
<label>
<br />
<br />
<input type="checkbox" name="fname7" value="1">
border 1</label>
<br>
<label>
<input type="checkbox" name="fname7" value="2">
border 2</label>
<br>
<label>
<input type="checkbox" name="fname7" value="3">
border 3</label>
<br />
<br /> <input type="checkbox" name="fname7" value="green">
green
<br>
<label>
<input type="checkbox" name="fname7" value="red">
red</label>
<br>
<label>
<input type="checkbox" name="fname7" value="yellow">
yellow</label>
<br />
</p>
<p>&nbsp; </p>
<p>
<input type="submit" name="submit" value="Click">
</p>
</form>

<?php

function do_it($str)
{

// set some variables
$host = "localhost";
$port = 12849;
$ret;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$ret = socket_connect($socket, $host, $port);
//$str = $str . "\n";
socket_write($socket, $str);
$input = socket_read($socket,1024);
$input = trim($input);
echo $input;
socket_close($socket);
}
?>

daveginorge

12:52 pm on Oct 29, 2009 (gmt 0)

10+ Year Member



You have included the for loop in the output string, try this.

$output = "<table style='color: $ary[6]; border: $ary[7]; background-color: $ary[8]; '>\n
<tr><td>" . $my_array[0] . "</td><td>" . $ary[1] . "</td></tr>\n";
$z = 2;
for ($i=0; $i < 2; $i++) {
$output .= "<tr><td>" . $ary[$z] . "</td>";
$output .= "<td>" . $ary[$z + 2] . "</td></tr>\n";
$z++;
}
$output .= "</table>\n";

Dave

mikejs

4:55 pm on Oct 29, 2009 (gmt 0)

10+ Year Member



Thanks for the reply I get no output back at the client3.php

it does now show the table ?

m

daveginorge

7:58 am on Oct 30, 2009 (gmt 0)

10+ Year Member



Hi Mike

What error(s) does your error log show?
Did you make the changes I posted yesterday, without them your php has errors and no output will occur.?
What program are you using to write php with? Does it have a debug mode? If so what is the error?

Try commenting out the sql sequence and filling the array with hard coded data, once you get that going re-introduce the sql section.

// reverse client input and send back
$output = "<table style='color: $ary[6]; border: $ary[7]; background-color: $ary[8]; '>\n";
$output .= "<tr><td>" . $my_array[0] . "</td><td>" . $ary[1] . "</td></tr>\n";
$z = 2;
for ($i=0; $i < 2; $i++) {
$output .= "<tr><td>" . $ary[$z] . "</td>";
$output .= "<td>" . $ary[$z + 2] . "</td></tr>\n";
$z++;
}
$output .= "</table>\n";

Good luck.

mikejs

9:41 am on Oct 30, 2009 (gmt 0)

10+ Year Member



Thanks for the reply I think I need something more like this

$output="<font color=\"#" . $elements[1] . "\" size=\"" . $elements[2] . "\"><table><tr><td>";
//add elements to output string
for ($counter = 3; $counter <= $noOfElements; $counter++){
//add element to output
$output = $output . $elements[$counter] . "</td><td>";
}
//close the output string
$output = $output . "</td></table></font>";

a full stop is used to concatenate elements of the string otherwise it will see $ary as data rather than a variable.

mikejs

9:51 am on Oct 30, 2009 (gmt 0)

10+ Year Member



I have stripped out the ary to leave

<?php
// set some variables
$host = "localhost";
$port = 23889;
// don't timeout!
set_time_limit(0);
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);

// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");

// close sockets
socket_close($spawn);
socket_close($socket);
}while ($input != "END");
?>

when I run this it works fine it reverses the output back to the client.php so I get a string like wolley 1 der goD droF WMB taC sraC which is reversed input I think I was getting confused with strings and arrays :-(

so it the $input that holds this string of data

daveginorge

4:52 pm on Oct 30, 2009 (gmt 0)

10+ Year Member



Just as a quick note mike

$output = $output . "This is concatenated";
can be written
$output .= "This is concatenated";

it's the short hand version and quicker to write.

mikejs

8:45 am on Nov 2, 2009 (gmt 0)

10+ Year Member



I still cannot get it to output the table :-(

$output="<font color=\"#" . $elements[1] . "\" size=\"" . $elements[2] . "\"><table><tr><td>";
//add elements to output string
for ($counter = 3; $counter <= $noOfElements; $counter++){
//add element to output
$output .= . $elements[$counter] . "</td><td>";

I have a string that needs to be re-output as a string but as part of the table if that makes sense..

}
//close the output string
$output .= . "</td></table></font>";

daveginorge

9:04 am on Nov 2, 2009 (gmt 0)

10+ Year Member



Hi Mike

I think you misunderstood the use of the short hand concatenation.

This is correct.
$output .= $elements[$counter] . "</td><td>";

This causes an error.
$output .= . $elements[$counter] . "</td><td>";

Are you developing against a local server on your PC or a remote server. You really need to be reading the Error Log to see what is giving you trouble.

mikejs

10:01 am on Nov 2, 2009 (gmt 0)

10+ Year Member



I am running the client /server php on wamp local machine I use dreamwever for my codding

the server is run then the client the idea is the client enters some info which then gets sent to the server it takes the string and puts it into a table that gets sent back to the client.php

daveginorge

12:46 pm on Nov 2, 2009 (gmt 0)

10+ Year Member



Locate the error log on your server. I assume your using Apache.
Program File/Apache Software Foundation/Apache2.2/logs/error
In there at the bottom will list the current error(s) sorted by date and time.

Also you could download the free version of php designer which has a debug program. I use the current paid version 7 but 2007 personal is free for personal use. Dreamweaver is ok for HTML but it lacks a lot for PHP.

If you send me a mail on here I will send you a link to the procedure I use for installing PHP, Apache & MySql on a Windows PC. I will not post it openly because of the posting rules.

mikejs

1:22 pm on Nov 2, 2009 (gmt 0)

10+ Year Member



Hi I have checked the error logs there is nothing showing for php just stop start reporting

I currently have

$output = "<table font color=\"" . $input[0,4] . "\" border\"" .$input[1]. "\" background-color \"" . $input[2]. "\">\n";
"\"<tr><td>\"" . $input[3] . "\" </td><td>\"" . $input[4] . "\"</td></tr>\n";
$z = 2;
for ($i=0; $i < 2; $i++) {
$output .= "<tr><td>" . $input[$z] . "</td>";
$output .= "<td>" . $input[$z + 2] . "</td></tr>\n";
$z++;
}
$output .= "</table>\n";

however the output is a 1 * 2 table with one letter from the string in each box I think as the string is not split I think I need to split the string first before I referance it $input is my string the table size and font colour do nothing it doesn't matter id i use a number or text

getting close just missing somthing currently the user input is being recived like this

hjgjhg ggggg aaaaa qqqqq eeeee rrrrrrrrrrrr I need each block to be the input for the table so do I need to put the string into an array first so i can then referance the array for the $input[0] $input[1] etc ?

mikejs

8:42 pm on Nov 2, 2009 (gmt 0)

10+ Year Member



Thanks for all the help so far I am very close to solving this this is the client / server code I have so far

Client


<html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit'])) {
$str = $_POST['fname'].' '. $_POST['fname1'].' '. $_POST['fname2'].' '.$_POST['fname3'].' '.$_POST['fname4'].' '.$_POST['fname5'].' '.$_POST['fname6'];

echo $str;

$exp_input = explode(' ', $str);

echo '<pre>';
print_r($exp_input);
echo '</pre>';

do_it($str);
}
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

<table width="325" border="0">
<tr>
<td width="129"><label>
Table Backround<br>
</label>
<label>
<input type="radio" name="fname" value="blue" id="TableColour_0">
Blue</label>
<br>
<label>
<input type="radio" name="fname" value="green" id="TableColour_1">
Green</label>
<br>
<label>
<input type="radio" name="fname" value="yellow" id="TableColour_2">
Yellow</label>
<br>
<label>
<input type="radio" name="fname" value="orange" id="TableColour_3">
Orange</label></td>
<td width="85">Table Border<br>
<label>
<input type="radio" name="fname1" value="1" id="fname2_0">
1</label>
<br>
<label>
<input type="radio" name="fname1" value="2" id="fname2_1">
2</label>
<br>
<label>
<input type="radio" name="fname1" value="3" id="fname2_2">
3</label>
<br>
<label>
<input type="radio" name="fname1" value="4" id="fname2_3">
4</label></td>
<td width="89"> Table Width <br>
<label>
<input type="radio" name="fname2" value="150" id="fname2_4">
150px</label>
<br>
<label>
<input type="radio" name="fname2" value="200" id="fname2_5">
200px</label>
<br>
<label>
<input type="radio" name="fname2" value="300" id="fname2_6">
300px</label>
<br>
<label>
<input type="radio" name="fname2" value="400" id="fname2_7">
400px</label></td>
</tr>
</table>
<p>
<input type="text" name="fname3" value="" >
<input type="text" name="fname4" value="" >
<input type="text" name="fname5" value="" >
<input type="text" name="fname6" value="" >
<input type="submit" name="submit" value="Click">
</p>
</form>

<?php

function do_it($str)
{
// set some variables
$host = "localhost";
$port = 19814;
$ret;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$ret = socket_connect($socket, $host, $port);
//$str = $str . "\n";
socket_write($socket, $str);
$input = socket_read($socket,1024);
$input = trim($input);
echo $input;
socket_close($socket);
}
?>
</body>
</html>

Server


<html>
<head>
</head>

<body>
<?php
// set some variables
$host = "localhost";
$port = 19814;
// don't timeout!
set_time_limit(0);
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) or die("Could not read input\n");

$exp_input = explode(' ', $input);

// clean up input string
//$input = trim($input);

$output = "<table bgcolor=\"" . $exp_input[0] . "\" border=\"" .$exp_input[1]. "\" width=\"" . $exp_input[2]. "\">\n";
"\"<tr><td>\"" . $exp_input[3] . "\" </td><td>\"" . $exp_input[4] . "\"</td></tr>\n";
"\"<tr><td>\"" . $exp_input[5] . "\" </td><td>\"" . $exp_input[6] . "\"</td></tr>\n";
$z = 2;
for ($i=0; $i < 2; $i++) {
$output .= "<tr><td>" . $exp_input[$z] . "</td>";
$output .= "<td>" . $exp_input[$z + 2] . "</td></tr>\n";
$z++;
}
$output .= "</table>\n";

//socket_write($socket, $str);
socket_write($spawn, $output, strlen($output)) or die("Could not write output\n");

echo $input;
socket_close($socket);
// close sockets
}while ($input != "END");

?>
</body>
</html>

when I run them server then client I enter some data and submit form and I get a table back, however it seems to be missing the last input box and it duplicates a diffrent array point

Input was
Array
(
[0] => blue
[1] => 2
[2] => 300
[3] => Dog
[4] => Cat
[5] => Car
[6] => Boat
)
I got a blue table with a 2px boarder 200px wide

in the boxes I got

</pre><table bgcolor="blue" border="2" width="300">
<tr><td>300</td><td>Cat</td></tr>
<tr><td>Dog</td><td>Car</td></tr>
</table>

why is it using the previous array[] value

I am confused so close but yet so far

is my loop correct?

$z = 2;
for ($i=0; $i < 2; $i++) {
$output .= "<tr><td>" . $exp_input[$z] . "</td>";
$output .= "<td>" . $exp_input[$z + 2] . "</td></tr>\n";
$z++;
}

thanks

M

mikejs

9:26 pm on Nov 2, 2009 (gmt 0)

10+ Year Member



Hi ok I fixed it I changed $z = 2; to $z = 3; but it will only output a 2 * 2 table even if I add more fields ?

any reson for this

I have

"\"<tr><td>\"" . $exp_input[5] . "\" </td><td>\"" . $exp_input[6] . "\"</td></tr>\n";
"\"<tr><td>\"" . $exp_input[7] . "\" </td><td>\"" . $exp_input[8] . "\"</td></tr>\n";

daveginorge

10:42 am on Nov 3, 2009 (gmt 0)

10+ Year Member



but it will only output a 2 * 2 table even if I add more fields ?

That is expected as $i=0; $i<2; $i++ means the for loop will only run twice giving you 2 rows. If you want more rows increase $i < 2 or try a while loop eg

This is just rough code you will need to tune for syntax errors
$z = 3;
while ($z < count($array)) {
$output .= "<tr><td>" . $exp_input[$z] . "</td>";
$output .= "<td>" . $exp_input[$z + 2] . "</td></tr>\n";
$z++;
}

This should keep producing rows until the end of the array.

mikejs

3:50 pm on Nov 3, 2009 (gmt 0)

10+ Year Member



Thanks for the reply I tried that I get no output ?

daveginorge

12:37 pm on Nov 4, 2009 (gmt 0)

10+ Year Member



I did say it was just rough code, meaning it would give you an idea to play on, you need to adapt it for your situation and correct any code errors.

If there is no output then there is an error, somewhere on your server the error has been written to a log, you need to find the log and check what the error is then you can correct the code.

The error log is normally in
C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs

if no helpful entries are found check your httpd-vhosts file for the location;
C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\extra

An example of an error entry

[Tue Nov 03 11:09:48 2009] [error] [client 10.70.70.101] PHP Notice: Undefined variable: db_con in D:\\Server\\html_docs\\vib\\content\\list_users.php on line 42, referer: [gladys...]

Tells me that on line 42 of file "list_users" I had a Undefined Variable
An error easy to find once the log is read.

mikejs

4:04 pm on Nov 4, 2009 (gmt 0)

10+ Year Member



I have looked in the log as mentioned and I see no php errors showing up

here is a snapshot not sure why the date is out but it was today

[Wed Dec 10 09:02:17 2008] [warn] pid file C:/Program Files/Apache Software Foundation/Apache2.2/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Wed Dec 10 09:02:17 2008] [notice] Apache/2.2.10 (Win32) configured -- resuming normal operations
[Wed Dec 10 09:02:17 2008] [notice] Server built: Oct 10 2008 12:39:04
[Wed Dec 10 09:02:17 2008] [notice] Parent: Created child process 3912
httpd.exe: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
httpd.exe: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[Wed Dec 10 09:02:17 2008] [notice] Child 3912: Child process is running
[Wed Dec 10 09:02:17 2008] [notice] Child 3912: Acquired the start mutex.
[Wed Dec 10 09:02:17 2008] [notice] Child 3912: Starting 64 worker threads.
[Wed Dec 10 09:02:17 2008] [notice] Child 3912: Starting thread to listen on port 80.
[Wed Dec 10 09:45:25 2008] [error] [client 127.0.0.1] File does not exist: C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/favicon.ico
[Wed Dec 10 09:49:45 2008] [notice] Parent: Received shutdown signal -- Shutting down the server.
[Wed Dec 10 09:49:45 2008] [notice] Child 3912: Exit event signaled. Child process is ending.
[Wed Dec 10 09:49:46 2008] [notice] Child 3912: Released the start mutex
[Wed Dec 10 09:49:49 2008] [notice] Child 3912: All worker threads have exited.
[Wed Dec 10 09:49:49 2008] [notice] Child 3912: Child process is exiting
[Wed Dec 10 09:49:49 2008] [notice] Parent: Child process exited successfully.

no error showing up do I need toe nable debug or somthing ?

would you know how i would add a style to


$output = "<table bgcolor=\"" . $exp_input[0] . "\" border=\"" .$exp_input[1]. "\"cellpadding=\"" .$exp_input[2]. "\" width=\"" . $exp_input[3]. "\">\n";
"<tr><td>\"" . $exp_input[4] . "\" </td><td>\"" . $exp_input[5] . "\"</td></tr>\n";
"<tr><td>\"" . $exp_input[6] . "\" </td><td>\"" . $exp_input[7] . "\"</td></tr>\n";

I have tried adding <td style="font-size : 10pt;">

to the <td> tag but it will not parse properly I know it all needs to join up concantination ?

daveginorge

11:39 am on Nov 5, 2009 (gmt 0)

10+ Year Member



I have tried adding <td style="font-size : 10pt;">
You need to escape the " to \"
I have tried adding <td style=\"font-size : 10pt;\">
Try
$output ="<td style=\"font-size: 10pt;\">";

Escaping means the parser will output this \" as " and not treat it as the end of the current "". You can also mix " and ' example

$output = '<table bgcolor="' . $exp_input[0] . '" border="' .$exp_input[1]. '"cellpadding="' .$exp_input[2]. '" width="' . $exp_input[3]. '">';

If the code snippet you posted is complete it won't parse correctly.

This is OK
$output = "<table bgcolor=\"" . $exp_input[0] . "\" border=\"" .$exp_input[1]. "\"cellpadding=\"" .$exp_input[2]. "\" width=\"" . $exp_input[3]. "\">\n";

The parser will have troble with the next 2 lines
"<tr><td>\"" . $exp_input[4] . "\" </td><td>\"" . $exp_input[5] . "\"</td></tr>\n";
"<tr><td>\"" . $exp_input[6] . "\" </td><td>\"" . $exp_input[7] . "\"</td></tr>\n";

should read
$output .= "<tr><td>\"" . $exp_input[4] . "\" </td><td>\"" . $exp_input[5] . "\"</td></tr>\n";
$output .= "<tr><td>\"" . $exp_input[6] . "\" </td><td>\"" . $exp_input[7] . "\"</td></tr>\n";

Good Luck

mikejs

12:17 pm on Nov 5, 2009 (gmt 0)

10+ Year Member



it outputs fine without adding the extra $output .=

thanks for all the help guy's its all done now my next task is a simple search engine I will post it up shortly

many thanks

Sorted Mike