PDA

View Full Version : dynamically creating rows and columns


WotNow
12-19-2009, 08:08 AM
Hi everyone, i am new to php. I have the following code to increment rows.

/// arg1 = row count
/// arg2 = table width
/// arg3 = border

function myfunc3($arg1,$arg2,$arg3){

$table = "<table width=\"$arg2\" border=\"$arg3\">
<tr>
<td>header 1</td>
<td>header 2</td>
<td>header 3</td>
</tr>";



for($i=0;$i<$arg1;$i++){


$table .= "<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>";

}

$table .= "</table>";


return $table;

}
Now i need to create another function that creates the columns - can someone give me some ideas, thanks.

tux
12-19-2009, 08:51 AM
Have a look at this code, enter your own details and see if you can understand it.

It should keep creating a new row in the table and populate it with the data from your database as long as the query returns a result.

<?php

require_once("path to your connection script here"); // database connection

$query = sprintf("SELECT * FROM yourtable"); // enter your table name here
$result = @mysql_query($query);
$data = @mysql_fetch_array($result);

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<table width="300" border="1" cellspacing="0" cellpadding="0">
<?php do{ ?>
<tr>
<td><?php echo $data['enter your 1st field here']; ?></td>
<td><?php echo $data['enter your 2nd field here']; ?></td>
<td><?php echo $data['enter your 3rd field here']; ?></td>
</tr>
<?php }while($data = @mysql_fetch_array($result)); ?>
</table>
</body>
</html>

Let me know how you get on.

Regards, Paul

WotNow
12-19-2009, 09:54 AM
So sorry should have explained this better, this is just an exercise not any code that I am using for any purpose. I am just local testing doing exercises.

I have done the first bit of code that increments the rows but I now have to create another function that increments the columns - this is my best guess ...

<?php
/// arg4 = column count
/// arg5 = table width
/// arg6 = border
function columns($arg4,$arg5,$arg6){

$table = "<table width=\"$arg2\" border=\"$arg3\">
<tr>
<td>header 1</td>
<td>header 2</td>
<td>header 3</td>
</tr>";



for($i=0;$i<$arg4;$i++){


$table .= "<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>";

}

Guess you have to concactanate??? function 3 and 4 to output???

Sorry no idea wot i am talking about. Thanks for any help

davidj
12-19-2009, 04:25 PM
HINT:

a nested for loop

one loop renders the rows the other renders the columns


for($i=0;$i<$arg1;$i++){

$table .= "<tr>";

for($j=0;$j<$arg2;$j++){

$table .= "<td>data</td>";

}

$table .= "</tr>";

}

Shucks

I gone and went and told you the answer

WotNow
12-19-2009, 07:44 PM
Thanks so much David, brilliant, it was driving me nuts, have been trying to work it out all day with my work partner. Should have seen some of the outputs, you would have had a larf, we did until we couldnt take it anymore and we couldnt wait till Tuesday to find out. Seriously this answer, did we cover "nested for loops?" or were just trying to drive me insane, lol? Will seriously study it. Frogs are sooo noisy, cyclone is passing us, very windy, very rainy. Speak to you Tuesday.

WotNow
12-19-2009, 08:44 PM
Hi david you said you gave me the answer, you gave me some of the answer but not all the answer. This for loop inside a loop is still defined in function myfunction3 ($arg1, $arg2, $arg3) {???

Still not getting it to work, must have a syntax error, this is what I have:

<?php
function myfunction3($arg1, $arg2, $arg3)
for ($i=0;$i<$arg1;$i++){

$table .- "<tr>";
for ($j=0;$j<$arg2;$j++){

$table .= "<td>data</td>";}
$table .= "</tr>";}

return $table;

echo my function3(15,'50%',1);
?>

I know I have one too many curly braces, this outputs a big fat nothing... so frustrating

davidj
12-19-2009, 09:39 PM
It only requires 2 arguments




function myfunction3($arg1, $arg2) {

$table = "<table width=\"80%\">";

for ($i=0;$i<$arg1;$i++){

$table .= "<tr>";

for ($j=0;$j<$arg2;$j++){

$table .= "<td>data</td>";

}

$table .= "</tr>";

}

$table .= "</table>";

return $table;

}



echo my function3(10,5); //<< change the values to see the magic

davidj
12-19-2009, 09:47 PM
your example....

Note the comments



function myfunction3($arg1, $arg2, $arg3)

for ($i=0;$i<$arg1;$i++){

// missing $table .= "<table>"

$table .- "<tr>"; //<< whats the - doing? and should be in the loop

for ($j=0;$j<$arg2;$j++){

$table .= "<td>data</td>";

}

$table .= "</tr>";

}

return $table;

//<< missing curly brace which ends the function

echo my function3(15,'50%',1); //<< too many values being passed

WotNow
12-20-2009, 04:16 AM
thanks david .. i think i love php