Forum Moderators: coopster

Message Too Old, No Replies

Dynamically creating html table based on array

         

oxidetones

1:09 am on Aug 3, 2007 (gmt 0)

10+ Year Member



Hi again, I got some help from you guys in this thread: [webmasterworld.com...] and would appreciate some more help...

I need to create a table based on parameters from a form on the previous page. The form sends up to 15 variables that are read out of the url into an array ($func). These variables refer to functions that a product may or may not have. This page should show which products have the functions chosen.

Normally when I create such a table in DW, its always based off the DB directly with SQL, with a filter on the URL parameter. I can't work out how to do the same when there are multiple parameters in the URL, or alternatively, pulling the parameters from the array.

Can someone point me in the right direction please?

Thanks, Adam

Habtom

9:54 am on Aug 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From what I gathered, you want to create tables on the fly. This is just like the other queries you have in your scripts.

CREATE TABLE ". $_REQUEST['table_name'] ."
(". $_REQUEST['column_1'] ." ". $_REQUEST['data_type_1'] .",
". $_REQUEST['column_2'] ." ". $_REQUEST['data_type_2'] .")

I hope that is what you are looking for. Obviously the statement is not tested, you need to do that :)

oxidetones

4:34 am on Aug 9, 2007 (gmt 0)

10+ Year Member



Hi Habtom, thanks for the reply, I think I may have been unclear though...

When I said create tables, I meant in the browser, not on the db. What I am trying to do is create a table that will show products. The products to be shown are defined by the functionality chosen on the previous page (FuncIDx). As an example, a user may choose "Media Player" and "GPS" as he wants to see all products with those functionalities. Each of those has a unique FuncID, which is in an array on the following page. The question I have is how can I call the list of products that has a DB entry in the column that corresponds to the FuncIDs?

Thanks, Adam

Habtom

11:36 am on Aug 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I missed your point. Though I have to admit now too, as much as I wanted to help you with this, I couldn't completly understand you.

From your first post:

I can't work out how to do the same when there are multiple parameters in the URL, or alternatively, pulling the parameters from the array.

if you wanted to access the array, you can see how it is structured by printing it as:

print_r($func);

and then use foreach or another loop to query something out of the database while accessing the values in the function one by one.

Habtom

jatar_k

12:17 am on Aug 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



can you show us a little code?

this is the bit I need a little more clarification on

>> I can't work out how to do the same when there are multiple parameters in the URL, or alternatively, pulling the parameters from the array.

a bit of code to illustrate what the differences are in the code when you have more than one param might make the error pop out

oxidetones

12:24 am on Aug 12, 2007 (gmt 0)

10+ Year Member



Hey Jatar, the URL with the variables is as follows:

MultiFunc.php?FuncID%5B%5D=FuncID05&FuncID%5B%5D=FuncID11&FuncID%5B%5D=FuncID44&submit=Go

Although this isnt pretty, its correct, the user chose three functionalities (FuncID's), 05, 11 and 44, which in turn refers to Camera, GPS and microSD card.

I've already checked that this works with the following code:

<?php foreach ( $_GET["FuncID"] as $func )
{
$find = array("FuncID01", "FuncID05", "FuncID06", "FuncID70", "FuncID10", "FuncID11", "FuncID18", "FuncID21", "FuncID22", "FuncID26", "FuncID27", "FuncID39", "FuncID44", "FuncID46", "FuncID47");
$replace = array("Wireless email", "Camera", "BlackBerry Maps", "Media player", "MMS", "GPS", "Trackball", "Trackwheel", "3.5mm jack", "Built-in speakerphone", "Bluetooth technology", "MP3 ringtones", "microSD card", "RIM wireless modem", "Tethered modem capability");

$func = str_replace($find, $replace, $func);
echo $func, "<br />";
}
?>

I am using Dreamweaver, which allows me to define a recordset based on one URL variable, and its getting products with all three (in this case) variables that I cant work out.

Thanks, and let me know if you need more info

jatar_k

12:29 am on Aug 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so is the problem that DW is messing up the query?

oxidetones

12:41 am on Aug 12, 2007 (gmt 0)

10+ Year Member



nope, there is no way in dw to call more than one param in a recordset, and I cant work out how to do it per hand.

What this page should be doing is listing products that have these functionalities. In a separate table where the product details are stored, there are columns named after each and every FuncID. Ideally, I want to show the following:

There are two products which have: Camera, GPS and microSD card: Product 1, Product 2

jatar_k

1:14 am on Aug 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ah, ok

then post the code that creates the query and the query itself

oxidetones

1:33 am on Aug 12, 2007 (gmt 0)

10+ Year Member



OK, this is the code for the form with the checkboxes:

<form action="MultiFunc.php" method="get" name="m">
<?php
do {
?>
<input type="checkbox" name="FuncID[]" value="<?php echo $row_rsMenu['FuncID']?>"> <?php echo $row_rsMenu['name']?> <br />
<?php
} while ($row_rsMenu = mysql_fetch_assoc($rsMenu));
$rows = mysql_num_rows($rsMenu);
if($rows > 0) {
mysql_data_seek($rsMenu, 0);
$row_rsMenu = mysql_fetch_assoc($rsMenu);
}
?>
<input name="submit" type="submit" value="Go" />
</form>

and this is how DW makes a recordset looking for 1 URL parameter (also called FuncID):

$colname_rsMultiFunc = "-1";
if (isset($_GET['FuncID'])) {
$colname_rsSingFunc = $_GET['FuncID'];
}
mysql_select_db($database_BBAddict, $BBAddict);
$query_rsMultiFunc = "SELECT id, Type, img, Name FROM bb_data WHERE $colname_rsMultiFunc = 'x' ORDER BY Type DESC";
$rsMultiFunc = mysql_query($query_rsMultiFunc, $BBAddict) or die(mysql_error());
$row_rsMultiFunc = mysql_fetch_assoc($rsMultiFunc);
$totalRows_rsMultiFunc = mysql_num_rows($rsMultiFunc);
?>

oxidetones

1:39 am on Aug 12, 2007 (gmt 0)

10+ Year Member



that last query was actually edited per hand, so its not as DW would have made it exactly.

The DB table in question is as follows:

id ¦ Type ¦ Name ¦ Series ¦ img ¦ FuncID01 ¦ FuncID02 ¦ etc, thru FuncID74

The individual rows are then the products themselves, and will have an "x" in the relevant FuncID columns if they possess that functionality.

Thanks again, Adam

jatar_k

2:46 am on Aug 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



just as a side note, DW sucks at php, talk about muddying the waters, just didn't want you to be confused about that ;)

you need to add an OR

select fieldname from tablename where col1='x' or col2='x' or col3='x';

that's how the query should work to get anything with those things checked to return. This means that a chunk of code needs to be changed.

you could do this

$whereclause = '';
$counter = 0;
foreach ($_GET['FuncID'] as $func) {
if ($counter!= 0) $whereclause .= ' OR ';
$whereclause .= $func . "='x'";
$counter++;
}
mysql_select_db($database_BBAddict, $BBAddict);
$query_rsMultiFunc = "SELECT id, Type, img, Name FROM bb_data WHERE $whereclause ORDER BY Type DESC";
$rsMultiFunc = mysql_query($query_rsMultiFunc, $BBAddict) or die(mysql_error());
$row_rsMultiFunc = mysql_fetch_assoc($rsMultiFunc);
$totalRows_rsMultiFunc = mysql_num_rows($rsMultiFunc);

might work, it's late and I am just shooting from the hip

oxidetones

3:03 am on Aug 12, 2007 (gmt 0)

10+ Year Member



i didnt know dw wasnt good at php, and uo until now its served well. I think the complexity of what Im trying to do may be too much for it.

Anyway, I inserted your query in place of the one I had in there. It doesnt show up in DW as a record set, so I cant validate it in DW. Online, whenusing the form that pulls up that page I get this:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '='x' OR ='x' ORDER BY Type DESC' at line 1

which is weird coz line 1 is justr the db connection....

Thanks again for your help on this

jatar_k

3:11 am on Aug 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the line 1 is the error from mysql and i sreferring to your query, so I made some kind of syntax error

try adding this

...
$query_rsMultiFunc = "SELECT id, Type, img, Name FROM bb_data WHERE $whereclause ORDER BY Type DESC";
echo '<p>the query is: ',$query_rsMultiFunc;
die();

$rsMultiFunc = mysql_query($query_rsMultiFunc, $BBAddict) or die(mysql_error());
$row_rsMultiFunc = mysql_fetch_assoc($rsMultiFunc);
$totalRows_rsMultiFunc = mysql_num_rows($rsMultiFunc);

and then paste what it echos

oxidetones

3:35 am on Aug 12, 2007 (gmt 0)

10+ Year Member



the query is: SELECT id, Type, img, Name FROM bb_data WHERE FuncID06='x' OR FuncID11='x' ORDER BY Type DESC

oxidetones

3:37 am on Aug 12, 2007 (gmt 0)

10+ Year Member



weird, I took out

echo '<p>the query is: ',$query_rsMultiFunc;
die();

and its fine, no error messages :)

Its still not shopwing up in DW as a recordset though, so Im not sure how I can call it

oxidetones

3:44 am on Aug 12, 2007 (gmt 0)

10+ Year Member



scratch that last comment, I should have thought first.... :)

Thanks again!

jatar_k

4:12 am on Aug 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I assume it worked

if so, my pleasure

oxidetones

4:53 am on Aug 12, 2007 (gmt 0)

10+ Year Member



it did,a nd one of my last two challenges with that website is complete!

I have no idea what you did (apart from code manually), I need to stop depending on DW I guess...

Thanks, Adam

Habtom

4:55 am on Aug 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo '<p>the query is: ',$query_rsMultiFunc;

If you still wanted this, you can replace the "," with a "." and it will display what you wanted.

But I guess that was only for a test.

oxidetones

5:02 am on Aug 12, 2007 (gmt 0)

10+ Year Member



no need, thanks. Ive just messaged you a link to the page where its used :)

jatar_k

12:06 pm on Aug 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how do you figure Habtom

the comma is legit
[php.net...]

I prefer no to have to concatenate my strings before echo can spit them out so I use commas.

learning more about the code DW is writing for you will help you in the long run oxidetones but using it while you are learning is just fine.

Habtom

1:35 pm on Aug 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the comma is legit

Indeed, that was my mistake. I immediately spotted the comma as the fault, as I normally (always) concatenate instead of displaying comma separated. From what I know comma is even supposed to be faster (correct me if I am wrong).

Habtom

jatar_k

1:37 pm on Aug 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it spits the parts out as it goes as opposed to having to concatenate them and then echo

so, yes, there is a miniscule speed advantage