jber Posted July 20, 2007 Share Posted July 20, 2007 A final question for today... So i want to view some fields of my table. The fields i want to view are passed via form (and correctly) to my final script. I want to show via table only the fields that have been selected. If no field has been selected it sends value NULL to the final script. For example, $selec = field1; $selec = NULL; I want to show in a final table only the column field 1, not the other one which will be called (in mysql terms) NULL The script that does not fully work (it stills shows the NULL column ) is this : <?php $selec = $_POST["selec"]; $selec2 = $_POST["selec2"]; $selec3 = $_POST["selec3"]; $selec4 = $_POST["selec4"]; $selec5 = $_POST["selec5"]; $selec6 = $_POST["selec6"]; $db = mysql_pconnect("localhost",$_SESSION["login"],$_SESSION["password"]); $query = " SELECT " .$selec. " ," .$selec2. " ," .$selec3." , " .$selec4." , " .$selec5. " ," .$selec6. " FROM " .$_SESSION["nomtab"]. " "; $result = mysql_query($query); echo "<table bgcolor=\"#DDDDDD\" align=center style=\"border:2px outset black\">"; for ($i = 0; $i < mysql_num_fields($result); $i++) { print "<th>".mysql_field_name($result, $i)."</th>\n"; } while ($registro = mysql_fetch_row($result)) { echo "<tr>"; foreach($registro as $clave) { echo "<td bgcolor=\"#BBBBBB\"style=\"border:2px groove black\" align=\"center\">",$clave,"</td>"; } } echo "</tr></table>"; } Thanks in advance. I´ve tried solutions based in using if but it does not work Jorge Quote Link to comment https://forums.phpfreaks.com/topic/61001-solved-a-little-more-help-with-presenting-data/ Share on other sites More sharing options...
MadTechie Posted July 20, 2007 Share Posted July 20, 2007 you lost me but if you using NULL then either use is_null or isset or ="NULL" ?? Quote Link to comment https://forums.phpfreaks.com/topic/61001-solved-a-little-more-help-with-presenting-data/#findComment-303528 Share on other sites More sharing options...
cooldude832 Posted July 20, 2007 Share Posted July 20, 2007 first off structurally your query is not to the standard idea try this and use the [ code ] [ / code ] (without spaces) please <?php $selec = $_POST["selec"]; $selec2 = $_POST["selec2"]; $selec3 = $_POST["selec3"]; $selec4 = $_POST["selec4"]; $selec5 = $_POST["selec5"]; $selec6 = $_POST["selec6"]; //Why do you need a persistent connection? $db = mysql_connect("localhost",$_SESSION["login"],$_SESSION["password"]); $query = "SELECT " .$selec. " ," .$selec2. " ," .$selec3." , " .$selec4." , " .$selec5. " ," .$selec6. " FROM " .$_SESSION["nomtab"]. " "; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result)){ echo "<table bgcolor=\"#DDDDDD\" align=center style=\"border:2px outset black\">"; foreach(mysql_field_name($result) as $value){ echo "<th>".$value."</th>\n"; } while ($row = mysql_fetch_row($result)) { echo "<tr>"; foreach($row as $value) { echo "<td bgcolor=\"#BBBBBB\"style=\"border:2px groove black\" align=\"center\">",$value,"</td>"; } } echo "</tr></table>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/61001-solved-a-little-more-help-with-presenting-data/#findComment-303539 Share on other sites More sharing options...
jber Posted July 20, 2007 Author Share Posted July 20, 2007 sorry bout the code issue, it won´t happen again Your solution does not do what i want, it outputs the same information (without fields name) and this two warnings Warning: Wrong parameter count for mysql_field_name() in C:\wamp\www\primeros_pasos\resultadofinal.php on line 72 Warning: Invalid argument supplied for foreach() in C:\wamp\www\primeros_pasos\resultadofinal.php on line 72 line 72 is this foreach(mysql_field_name($result) as $value){ echo "<th>".$value."</th>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/61001-solved-a-little-more-help-with-presenting-data/#findComment-303554 Share on other sites More sharing options...
per1os Posted July 20, 2007 Share Posted July 20, 2007 <?php $selec = isset($_POST["selec"])?$_POST["selec"] . ",":''; $selec2 = isset($_POST["selec2"])?$_POST["selec2"] . ",":''; $selec3 = isset($_POST["selec3"])?$_POST["selec3"] . ",":''; $selec4 = isset($_POST["selec4"])?$_POST["selec4"] . ",":''; $selec5 = isset($_POST["selec5"])?$_POST["selec5"] . ",":''; $selec6 = isset($_POST["selec6"])?$_POST["selec6"] . ",":''; $select_cols = $selec . $selec2 . $selec3 . $selec4 . $selec5 . $selec6; $select_cols = substr($select_cols, 0, -1); $db = mysql_pconnect("localhost",$_SESSION["login"],$_SESSION["password"]); $query = " SELECT " .$select_cols . " FROM " .$_SESSION["nomtab"]. " "; $result = mysql_query($query); echo "<table bgcolor=\"#DDDDDD\" align=center style=\"border:2px outset black\">"; for ($i = 0; $i < mysql_num_fields($result); $i++) { print "<th>".mysql_field_name($result, $i)."</th>\n"; } while ($registro = mysql_fetch_row($result)) { echo "<tr>"; foreach($registro as $clave) { echo "<td bgcolor=\"#BBBBBB\"style=\"border:2px groove black\" align=\"center\">",$clave,"</td>"; } } echo "</tr></table>"; } Why not only pass in the columns you want out of the query? This should suffice. Quote Link to comment https://forums.phpfreaks.com/topic/61001-solved-a-little-more-help-with-presenting-data/#findComment-303565 Share on other sites More sharing options...
jber Posted July 20, 2007 Author Share Posted July 20, 2007 Thanks for help provided but it does not work for me this is the code when i sent null echo '<option value="NULL"></option>'; the code you sent me outputs this warnings and does not output anything Warning: mysql_num_fields(): supplied argument is not a valid MySQL result resource in C:\wamp\www\primeros_pasos\resultadofinal.php on line 104 Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\wamp\www\primeros_pasos\resultadofinal.php on line 108 Quote Link to comment https://forums.phpfreaks.com/topic/61001-solved-a-little-more-help-with-presenting-data/#findComment-303588 Share on other sites More sharing options...
Barand Posted July 20, 2007 Share Posted July 20, 2007 Here's one approach to this <?php if (isset($_POST['sub'])) { if (isset($_POST['selec'])) { $fields = join(', ', $_POST['selec']); $sql = "SELECT $fields FROM client_address"; echo "<pre>$sql</pre>"; } else { echo "No fields selected"; } } ?> <form method='post'> Choose fields to display<br/> <input type="checkbox" name="selec[]" value="CAID"> CAID<br/> <input type="checkbox" name="selec[]" value="CID"> CID<br/> <input type="checkbox" name="selec[]" value="cityid"> City id<br/> <input type="checkbox" name="selec[]" value="street"> Street<br/> <input type="checkbox" name="selec[]" value="postcode"> Postcode<br/> <input type="submit" name="sub" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/61001-solved-a-little-more-help-with-presenting-data/#findComment-303620 Share on other sites More sharing options...
per1os Posted July 20, 2007 Share Posted July 20, 2007 Ok now, let's try to think here. How can we test this out? <?php $selec = isset($_POST["selec"])?$_POST["selec"] . ",":''; $selec2 = isset($_POST["selec2"])?$_POST["selec2"] . ",":''; $selec3 = isset($_POST["selec3"])?$_POST["selec3"] . ",":''; $selec4 = isset($_POST["selec4"])?$_POST["selec4"] . ",":''; $selec5 = isset($_POST["selec5"])?$_POST["selec5"] . ",":''; $selec6 = isset($_POST["selec6"])?$_POST["selec6"] . ",":''; $select_cols = $selec . $selec2 . $selec3 . $selec4 . $selec5 . $selec6; $select_cols = substr($select_cols, 0, -1); echo 'Select Cols1: ' . $select_cols; ?> Does that print anything out? If yes, post it if not try this: <?php $selec = (isset($_POST["selec"]) && $_POST['selec'] != "NULL")?$_POST["selec"] . ",":''; $selec2 = (isset($_POST["selec2"]) && $_POST['selec2'] != "NULL")?$_POST["selec2"] . ",":''; $selec3 = (isset($_POST["selec3"]) && $_POST['selec3'] != "NULL")?$_POST["selec3"] . ",":''; $selec4 = (isset($_POST["selec4"]) && $_POST['selec4'] != "NULL")?$_POST["selec4"] . ",":''; $selec5 = (isset($_POST["selec5"]) && $_POST['selec5'] != "NULL")?$_POST["selec5"] . ",":''; $selec6 = (isset($_POST["selec6"]) && $_POST['selec6'] != "NULL")?$_POST["selec6"] . ",":''; $select_cols = $selec . $selec2 . $selec3 . $selec4 . $selec5 . $selec6; $select_cols = substr($select_cols, 0, -1); echo 'Select Cols2: ' . $select_cols; ?> Report back what those 2 print out. The reason it was invalid resource was the query was bad. You could add this to the mysql_query part $result = mysql_query($query) or DIE(mysql_error()); But no need to in that the error is in the select statement, so report back what that shows. Quote Link to comment https://forums.phpfreaks.com/topic/61001-solved-a-little-more-help-with-presenting-data/#findComment-303621 Share on other sites More sharing options...
cooldude832 Posted July 20, 2007 Share Posted July 20, 2007 frost replace that bloated code with <?php for($i=1,$i<=6;$i++){ if(!empty($_POST['selec'.$i])){ $selec[$i] = ", ".$_POST['selec'.$i]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/61001-solved-a-little-more-help-with-presenting-data/#findComment-303686 Share on other sites More sharing options...
jber Posted July 21, 2007 Author Share Posted July 21, 2007 I´ll try to post what it outputs in each of your suggests (that are very very appreciated) (sorry for my english, i´m spanish). So, out of the first frost110 it outputs what i want only in the case where the first option (selec) is NULL. Whenever any other of the selec2, selec3,... is null it will output a column headed by NULL Out of your second one (first part): The query was : SELECT NULL,comentarioCO,comentarioITL,comentarioPGRS,NULL,NULL FROM semana1 Explanation : only .selec2., selec3 and selec4 have a value different to NULL. So it does not work properly (second part) IT WORKS CORRECTLY. Thank you so so so much Also thanks to cooldude832 !!! Topic solved Quote Link to comment https://forums.phpfreaks.com/topic/61001-solved-a-little-more-help-with-presenting-data/#findComment-304018 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.