...

View Full Version : Dynamic table problem...



oorik
03-18-2004, 02:34 AM
I have a problem.

The MySQL database has over 400 rows and 23 colums.

The user can view all 400+ rows if desired. No prob with that.

The user can choose which of the 23 colums of data to view,
from a minimum of 3 columns to the full 23.

I'm using checkboxes to determine which columns the user
wants to view in the results table.

I can't figure out how to dynamically create the table from
user input...

Any help would be greatly appreciated.

Thanks...

sad69
03-18-2004, 07:16 PM
Well the checkboxes are going to be in the table header I assume? This will have to be a form, and there will have to be some sore of submit button which will post the form variables back to this page (or to some other page...?).

Based on whichever checkboxes come through (if you didn't know, if a checkbox is not checked, it will not be posted at all), you'd know which columns to show. You'd have to build the proper query for that and store the result in $result.

Then to create the table, you'd do something like this:


<table>
<tbody>
<?php
while($row = mysql_fetch_array($result)) {
?>
<tr>
<?php
foreach($row as $columnName=>$columnValue) {
?>
<td>
<?php echo $columnValue; ?>
</td>
<?php
}//foreach
?>
</tr>
<?php
}//while
?>
</tbody>
</table>


If that doesn't make sense, or doesn't work -- sorry! Hopefully that will point you in the right direction though..

Hope that helps,
Sadiq.

oorik
03-19-2004, 12:33 AM
All of your assumptions are correct.

I'm new at PHP, obviously, and I knew
I'd have to build the query from the
checkboxes. (I'm still figuring that out
too, lol)

I just couldn't seem to get a mental
picture of what to do and there didn't
seem to be any questions like mine in
any of the forums I searched.

Your code helps a great deal.

I'll post again when I figure out how to
build the query. It shouldn't be too hard
using a foreach statement.

Thanks, Sadiq

oorik
03-21-2004, 11:41 PM
do {
echo "<tr>";
$colcount = 1;
foreach($result as $value) {
if ($colcount == 1) {
echo "<td bgcolor='#333366' align=left><font size=1 color=lightyellow face='arial narrow'>" . " &nbsp; " . $myrow[$value] . "</font></td>"; }
elseif ($myrow[$value] == '0.000') {
echo "<td bgcolor='#333366'></td>"; }
else {
echo "<td bgcolor='#333366' align=center><font size=1 color=lightyellow face='arial narrow'>" . $myrow[$value] . "</font></td>"; }
$colcount = $colcount + 1;
}
echo "</tr>";
}
while ($myrow = mysql_fetch_array($answer));
echo "</tr></table>\n";



Now, of course, I have another problem...

If the user doesn't click a checkbox it still runs
but delivers a warning. I'd rather validate first,
immediately after 'submit' is clicked but can't
seem to get any validation to work.

I'd like to post a message to 'click something'
and stop processing the script , just return
them to the form page.

Any suggestions?

sad69
03-22-2004, 05:59 AM
Javascript form validation perhaps?

I wonder if this will help:
http://www.codeave.com/javascript/code.asp?u_log=7062

Is your application online? If you give us a link, perhaps we can see what you're actually talking about and help you carve a solution.

Good luck,
Sadiq.

missing-score
03-22-2004, 07:39 AM
Hi, oorik. Please could you use PHP tags when posting code, you can read about that in a tutorial I wrote: <http://www.freeipb.net/phptags>

Anyway, to your problem, try something like this:


<p>
<input type="checkbox" name="selections[]" value="column_name />View Column Name<br />
<input type="checkbox" name="selections[]" value="colum_name2" />View Column Name 2
</p>

(you would need to obviously add more checkboxes, but notice how the name is exactly the same for each one, "selections[]".

Then for your PHP code:


<?php

if(count($_POST['selections']) < 3){

echo "You need to enter more fields to view";

} else {

$slfrom = "";

foreach($_POST['selections'] as $fieldname){

$slfrom .= $fieldname.", ";

}

$slfrom = substr_replace($slfrom, "", -2);

$query = "SELECT FROM ".$slfrom." WHERE blah...."; // insert actual end of query

$printheader = 1; // tells it whether to do a header or not

if($data = mysql_query($query)){

while($row = mysql_fetch_assoc($data)){

foreach($row as $columName => $columnValue){

// print data

}

}

} else {

echo "mysql query failed";

}

}

Hope this helps you a bit... sorry for leaving it a bit incomplete but ive got to go to school :(

oorik
03-22-2004, 01:28 PM
I considered javascript but don't quite know how it works.

For example, does Java validate when the submit button
is clicked? If so, there'll be 2 things happening upon a
submit... The javascript will run AND the results from the
form will be sent to php. Both will happen simultaneously.

I can't wrap my mind around how it would validate before
sending the form data.

I thought about simply suppressing the warnings... That just
hides the problem, I'd rather fix it.

I'll try the code from missing-score. Just the first line gives me
an idea. However, if the count is 0 i'd need a notice to the user
with an 'OK' button that redirects to the form page. Can php do
that? If the count is anything but 0, it's good to go.

Here's the link. (http://www.oorik.net/working/searchtest.html)

Thanks people.

sad69
03-22-2004, 05:58 PM
There's an event recognized by Javascript called onSubmit. You would assign a function to be called onSubmit.

Now what happens is that when the form is submitted, this function is called. If this function returns true (meaning it's okay to submit), the form will be submitted. If this function returns false, then the form will not submit, leaving you on the current page.

This is shown in that link I left in my previous post.

Hope that helps,
Sadiq.

oorik
03-23-2004, 05:13 PM
I may have done something wrong but I don't
think so... Probably missing something simple.

It's not executing any of the javascript. At least
it doesn't appear to be executing it.

Go to the link I gave you above then 'view/source'.

The submit button still sends to php.

Hmmm...

I think this is the only javascript I've ever used
that didn't work right away, lol.

Prob just me...

Please look and tell me what I've overlooked...

Thanks again, this time for your patience.

sad69
03-23-2004, 05:52 PM
Sorry it took me a bit to reply.. stupid IE wouldn't show the source because I had 'too many' temporary internet files...

Anyways, looks like you're missing some closing braces in your Javascript:


function checkbox_checker() {

// set var checkbox_choices to zero

var checkbox_choices = 0;

// Loop from zero to the one minus the number of checkbox button selections
for (counter = 0; counter < classsearch.checkbox.length; counter++) {

// If a checkbox has been selected it will return true
// (If not it will return false)
if (classsearch.checkbox[counter].checked) {
checkbox_choices = checkbox_choices + 1;
}
}

if (checkbox_choices == 0 ) {
// If there were no selections made display an alert box
msg="You must select at least one checkbox.\n\n"
msg=msg + "Please try again."
alert(msg)
return (false);
}
else {
// If there were one or more selections made return = true
// alert("Please make three selections. \n" + checkbox_choices + " entered so far.")
return (true);
}
}


I made a few other changes, namely:
-changed = to ==. = is an assignment, whereas == checks for equality: a = b: a stores the value b stored. a == b: is a storing the same value as b? true or false....
-combined your two if statements into one if/else statement. this is because of the return statements and the fact that checkbox_choices will either be 0 or greater, so if is 0 else is greater.

Hope that makes sense. I didn't inspect your code much more than that, but I expect that will at least get your code running.

If you're using IE, then you can check your Javascript console (bottom left corner of your browser. if there is a yellow triangle, there's a problem and it'll tell u the line number and some lame message..)

EDIT: BTW, in the future, when coding Javascript, or PHP for that matter, you should try to indent your code as much as possible, that way you know what braces line up with what and which blocks of code belong to what, etc... just a thought (after viewing the source of your webpage..)

Hope that helps,
Sadiq.

oorik
03-24-2004, 04:47 AM
I copy/pasted your altered code over my old code,
(which was copy/pasted from the site you entered
above, with all the mistakes), I simply changed a
few numbers.

It still doesn't work.

The error message says:

"classsearch.checkbox.length" is null or doesn't exist.

for (counter = 0; counter < classsearch.checkbox.length; counter++)

So I temporarily deleted the '.length' part... The script
rane fine, with the small exception that the alert was
shown whether any checkbox was clicked or not. I put
the '.length' portion back.

Is '.length' what we want there? I don't know enough
about javascript to determine whether or not that piece
of code is good.

The guy who wrote that code used = for ==, maybe he
used the wrong thing there too...?!?

sad69
03-24-2004, 06:27 AM
Umm...

Ok the problem is that you've given all the checkboxes the same name. At this point, since the data has not yet been submitted to the server, the names are merely strings. Since you've given all the checkboxes the same name, you cannot refer to them individual by their name.

I know it looks like you've created an array, but that array will not 'exist' (for lack of a better word) until it is received by the server.

If you can, I would give each checkbox a unique id, and use a loooong if statement to test if any of the checkboxes have been checked.

Another way, would be to use DOM to 'loop' through your checkboxes.

The DOM method actually sounds good. Try the following:

Replace:


<tr>
<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>General</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="slot"><font face='arial' size=-1 color=lightyellow>&nbsp;Slot &nbsp; &nbsp;</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set1"><font face='arial' size=-1 color=lightyellow>&nbsp;Set1</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set2"><font face='arial' size=-1 color=lightyellow>&nbsp;Set2 &nbsp;</font></td>

<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set3"><font face='arial' size=-1 color=lightyellow>&nbsp;Set3</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="con"><font face='arial' size=-1 color=lightyellow>&nbsp;Con &nbsp; </font></td>
</tr>
<tr>
<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Tactics</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>

<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Air &nbsp;</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>
</tr>
<tr>
<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Attack</font></td>

<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Air &nbsp;</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>
</tr>
<tr>

<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Defend</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Air &nbsp;</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>
</tr>



with:


<tbody id="tb_checkboxes">
<tr>
<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>General</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="slot"><font face='arial' size=-1 color=lightyellow>&nbsp;Slot &nbsp; &nbsp;</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set1"><font face='arial' size=-1 color=lightyellow>&nbsp;Set1</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set2"><font face='arial' size=-1 color=lightyellow>&nbsp;Set2 &nbsp;</font></td>

<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set3"><font face='arial' size=-1 color=lightyellow>&nbsp;Set3</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="con"><font face='arial' size=-1 color=lightyellow>&nbsp;Con &nbsp; </font></td>
</tr>
<tr>
<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Tactics</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>

<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Air &nbsp;</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>
</tr>
<tr>
<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Attack</font></td>

<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Air &nbsp;</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>
</tr>
<tr>

<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Defend</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Air &nbsp;</font></td>
<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>
</tr>
</tbody>


Then use:


function checkbox_checker() {
var checkbox_choices = 0;

var tb = document.getElementById("tb_checkboxes");
for(var i = 0; i < tb.childNodes.length; i++) {
var tr = tb.childNodes.item(i);
for(var j = 1; j < tr.childNodes.length; j++) {
if(tr.childNodes.item(j).firstChild.checked) {
checkbox_choices++;
}
}
}

if(checkbox_choices == 0) {
// If there were no selections made display an alert box
msg="You must select at least one checkbox.\n\n"
msg=msg + "Please try again."
alert(msg)
return (false)
}
else {
// If there were one or more selections made return = true
// alert("Please make three selections. \n" + checkbox_choices + " entered so far.")
return (true);
}
}


Again I haven't tested this code because I'm just falling asleep... hopefully that works out for you. Post back any error messages, and I'll help you debug it.

Nite,
Sadiq.

oorik
03-24-2004, 04:07 PM
It worked !!

I added two tags:

<tbody id="tb_checkboxes">

and

</tbody>

Then changed the check function to what you
gave me... Excellent work, Sidiq.

I wish there was a way to do this strictly using
php... It seems that having to learn 3 different
languages to accomplish one task is a bit much.

Now to figure out how to use 'style' to reduce the
amount of code... And learn Java... lolol.

I can't honestly tell you how much I appreciate
your assistance in this little 'learning experience'.

Thank you...

sad69
03-24-2004, 05:55 PM
Hey no problem!

What you will find is that PHP is only limited to server-side processing. So unless you want to send information from the client to the server to process it and then back, everytime, then it's a good idea to use client-side scripting, which Javascript provides.

If you were to use purely PHP, then it seem that the client's browser kept refreshing which can be annoying..

Of course now you run into the problem where some of your visitors may have Javascript disabled or are running some weird browser that doesn't have a proper version of Javascript installed or something. But these days it isn't too common...

It's hard to win everytime, but sometimes it doesn't hurt to have a few tools in your belt to solve various problems.

Anyway, good luck!
Sadiq.

oorik
03-28-2004, 03:19 PM
Ok....

Checkbox validation works wonderfully...

For 3 days I haven't been able to alter
the validation routine to work with text-
boxes...

Searching google and other places shows
me how to validate a single textbox...

I have 15 on a single form (this is all for
the same site as the problem in my other
posts...).

Of these textboxes, again, only 1 need
be filled to pass to php and only the
numbers '0 - 9' and the decimal point '.'
are legal.

Is there a way (Onblur) to validate on the
fly and relocate the cursor to the incorrect
textbox (focus), before the submit button
is clicked?

As you can see, I have done a little digging
but your code is so short and 'to the point',
I was hoping I could simply adapt it. Alas,
it's hard and time consuming searching the
web, although I tried...

Any help would be greatly appreciated...
(again), lol.

sad69
03-28-2004, 11:00 PM
Again I think I'd need to see the link or the code of the page with the textboxes to be of help.

Post that and I'll see what I can do.

Sadiq.

oorik
03-28-2004, 11:35 PM
It's not connected to anything yet,
I just uploaded it so you could see it.

The php isn't even written...

Click here (http://www.oorik.net/jrkakn/calculate.html)

sad69
03-29-2004, 05:50 PM
hey i like the new look of codingforums.com!

further, oorik, here's what i think for your textbox validation:



function textbox_checker() {
var inputs = document.getElementsByTagName("INPUT");

var i;
var blankExists = false;
for(i = 0; i < inputs.length; i++) {
if(inputs[i].getAttribute("type") == "text") {
if(input[i].value == "") {
blankExists = true;
break;
}
}
}

if(blankExists) {
inputs[i].focus();
return false;
}
else {
return true;
}
}


Hopefully that does the trick. Post back if you're having some troubles.

Sadiq.

oorik
03-30-2004, 12:25 PM
It looks like your code returns
false if ANY textbox is blank...

I need it to return false only if
they are ALL blank. If even one
is not blank, it's a go...

Am I reading this wrong?

It only needs to set focus on a
textbox in which the entry is not
a number. Valid characters are
0 - 9 and the decimal point.

Would 'isNumber' work for that?

sad69
03-30-2004, 05:28 PM
Oops, read your request wrong... sorry! You're right, mine will die on at least one blank.

In that case, try this:


function textbox_checker() {
var inputs = document.getElementsByTagName("INPUT");

var i;
var atLeastOneFilled = false;
for(i = 0; i < inputs.length; i++) {
if(inputs[i].getAttribute("type") == "text") {
if(input[i].value != "") {
atLeastOneFilled = true;
break;
}
}
}

if(atLeastOneFilled) {
return true
}
else {
//do any focus stuff you want here... maybe input[0].focus();
return false;
}
}


I think that should work, just altered my old code a little.

Let me know how that works out then,
Sadiq.

sad69
03-30-2004, 05:42 PM
Forgot to you answer your question regarding isNumber.

I would use a regular expression. The reason being that it will hold text, not really a number (although you could cast it I suppose...). So something like:



<input type="text" name="someTextBox" onChange="validateNumber(this)"/>

function validateNumber(txtBox) {
if(/[0-9]*/.test(txtBox.value)) {
return true;
}
else {
txtBox.value = ""; //blanks out the box
return false;
}
}


I don't know how you want to handle telling the user there's been erroneous input. I normally just blank it out. You could also use a pop-up.

If you don't want to have the textbox validated after input, you can run this function on each input[] in the loop in the function in my previous post. So that for loop would look something like this:


for(i = 0; i < inputs.length; i++) {
if(inputs[i].getAttribute("type") == "text") {
validateNumber(inputs[i]);
if(input[i].value != "") {
atLeastOneFilled = true;
break;
}
}
}


Hope that works out!

Sadiq.

oorik
04-22-2004, 04:11 PM
Sorry for the long response time...

Changed residences, changed servers, etc...

Anyway, the script passes whether any
troop boxes are filled or not...

Remember, only the input boxes for troops
need to be tested... At least one must be
non-empty.

www.oorik.net/test/calculate.html

New server uses php 4.0 also, darnit...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum