Jump to content

Recommended Posts

 

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

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>";
}
?>

 

 

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"; 

<?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.

 

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

 

 

 

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>

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.