473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How can I get just one row from selected column?

Hi.
How can I get just one row from selected column and put it into html
dropdown list
I tried like this:

function pobierz_wszystko($tabela,$kolumna)
{
$zapytanie="SELECT $kolumna FROM $tabela";
$wynik=mysql_query($zapytanie);
while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz>$wiersz</option> <br />";
}
}

<select name="$hname">
<option value="*" selected >All</option>
<?php
pobierz_wszystko('hotel','hotel_nazwa');
?>
</select>
But it doesn't work properly because I'm getting a dropdown list with
All,and Array,Array,Array,Array,Array
Instead of Array I'd like to have a value from a row.

Is it because I'm using mysql_fetch_array($wynik,MYSQL_ASSOC)?
or maybe the query is wrong?

Thanks
Leszek
Dec 29 '05 #1
9 1637
If you use mysql_fetch_array($wynik,MYSQL_NUM)
you can write $wiersz[0] to access the value.
Also, if the value contains spaces, your option HTML will probably not
work as expected.

Leszek wrote:
Hi.
How can I get just one row from selected column and put it into html
dropdown list
I tried like this:

function pobierz_wszystko($tabela,$kolumna)
{
$zapytanie="SELECT $kolumna FROM $tabela";
$wynik=mysql_query($zapytanie);
while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz>$wiersz</option> <br />";
}
}

<select name="$hname">
<option value="*" selected >All</option>
<?php
pobierz_wszystko('hotel','hotel_nazwa');
?>
</select>
But it doesn't work properly because I'm getting a dropdown list with
All,and Array,Array,Array,Array,Array
Instead of Array I'd like to have a value from a row.

Is it because I'm using mysql_fetch_array($wynik,MYSQL_ASSOC)?
or maybe the query is wrong?

Thanks
Leszek

Dec 29 '05 #2
Leszek wrote:
Hi.
How can I get just one row from selected column and put it into html
dropdown list
I tried like this:

function pobierz_wszystko($tabela,$kolumna)
{
$zapytanie="SELECT $kolumna FROM $tabela";
$wynik=mysql_query($zapytanie);
while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz>$wiersz</option> <br />";
}
}

<select name="$hname">
<option value="*" selected >All</option>
<?php
pobierz_wszystko('hotel','hotel_nazwa');
?>
</select>
But it doesn't work properly because I'm getting a dropdown list with
All,and Array,Array,Array,Array,Array
Instead of Array I'd like to have a value from a row.

Is it because I'm using mysql_fetch_array($wynik,MYSQL_ASSOC)?
or maybe the query is wrong?

Thanks
Leszek


http://www.php.net/manual/en/functio...etch-array.php

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 29 '05 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Leszek wrote:
while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz>$wiersz</option> <br />";
}


Try something like

$value = $wiersz[$kolumna];
echo "<option value='$value'>$value</option> <br />";

(remember to enclose the value parameter of the <option> tag in quotes, or
your XHTML code will be invalid)

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDtBgq3jcQ2mg3Pc8RAlVWAJ9iY0kHAQjAkLrG8QStj2 v3dLLKQACeKV5i
7kYsmBjlAT92TX1ejcCcpqw=
=ehjI
-----END PGP SIGNATURE-----
Dec 29 '05 #4
>> while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz>$wiersz</option> <br />";
}


Try something like

$value = $wiersz[$kolumna];
echo "<option value='$value'>$value</option> <br />";

(remember to enclose the value parameter of the <option> tag in quotes, or
your XHTML code will be invalid)

To make sure it's valid, you should also use "htmlspecialchars"
function (if you use double-quotes around attribute values,
then you do not have to specify the second parameter for this function,
but if you use single-quotes - as in the example above - then you
should specify that "htmlspecialchars" should also escape single-quotes).
Hilarion
Dec 30 '05 #5
Leszek wrote:
Hi.
How can I get just one row from selected column and put it into html
dropdown list
I tried like this:

function pobierz_wszystko($tabela,$kolumna)
{
$zapytanie="SELECT $kolumna FROM $tabela";
$wynik=mysql_query($zapytanie);
while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz>$wiersz</option> <br />";
}
}


The query is wrong among other things.

proper: Select $kolumna from $tabela limit 1

You can also use an offset, reference the manual at
http://dev.mysql.com/doc/refman/4.1/en/select.html

Another thing is, if you are *always* going to want just one result, not
only should you use the proper select, but you should also limit your code
to only ask for 1 result:

$wynik=mysql_query($zapytanie);
$wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC)
echo "<option value=$wiersz>$wiersz</option> <br />";

notice the lack of using a "while" statement, which is not appropriate for 1
result queries.

-Dave

Jan 3 '06 #6
>> How can I get just one row from selected column and put it into html
dropdown list
I tried like this:

function pobierz_wszystko($tabela,$kolumna)
{
$zapytanie="SELECT $kolumna FROM $tabela";
$wynik=mysql_query($zapytanie);
while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz>$wiersz</option> <br />";
}
}


The query is wrong among other things.

proper: Select $kolumna from $tabela limit 1

You can also use an offset, reference the manual at
http://dev.mysql.com/doc/refman/4.1/en/select.html

Another thing is, if you are *always* going to want just one result, not
only should you use the proper select, but you should also limit your code
to only ask for 1 result:

$wynik=mysql_query($zapytanie);
$wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC)
echo "<option value=$wiersz>$wiersz</option> <br />";

notice the lack of using a "while" statement, which is not appropriate for 1
result queries.

I think that Leszek wanted to ask "how can I get just one COLUMN from...",
in which case LIMIT clause will not be what he looks for.
As others explained - Leszek used the PHP mysql functions output in
a wrong way, and it had nothing to do with SQL syntax.
Hilarion
Jan 4 '06 #7

"Hilarion" <hi******@SPAM.op.SMIECI.pl> wrote in message
news:dp**********@news.onet.pl...
How can I get just one row from selected column and put it into html
dropdown list
I tried like this:

function pobierz_wszystko($tabela,$kolumna)
{
$zapytanie="SELECT $kolumna FROM $tabela";
$wynik=mysql_query($zapytanie);
while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz>$wiersz</option> <br />";
}
}
The query is wrong among other things.

proper: Select $kolumna from $tabela limit 1

You can also use an offset, reference the manual at
http://dev.mysql.com/doc/refman/4.1/en/select.html

Another thing is, if you are *always* going to want just one result, not
only should you use the proper select, but you should also limit your
code to only ask for 1 result:

$wynik=mysql_query($zapytanie);
$wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC)
echo "<option value=$wiersz>$wiersz</option> <br />";

notice the lack of using a "while" statement, which is not appropriate
for 1 result queries.

I think that Leszek wanted to ask "how can I get just one COLUMN from...",
in which case LIMIT clause will not be what he looks for.
As others explained - Leszek used the PHP mysql functions output in
a wrong way, and it had nothing to do with SQL syntax.

Actually, he wanted "just one row from a selected column". If he was
speaking about a *particular* row, I would suggest a WHERE clause (SELECT
$kolumna FROM $tablea WHERE id=5), or a counter+if approach.
I am not sure exactly what it is he is trying to do.. maybe trying to pick
out a row to put a SELECTED attribute on the <option> on or something?
(speak up!)
$count=1;
while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz>$wiersz</option> <br />";
if (4==count) {
do something here
}
$count++;
}

the problem with this is, how are you going to guarantee that there will
always be more than 4 rows, if this is what he is really asking?

And if he is wanting just the first row from a SELECT, yeah, a LIMIT 1 would
be good to append on the statement. but I would suggest the following code
below in case you get no rows (you can drop the else part if you want):
He should remove the <br /> tag out of the <select></select> area - it
should not be beside an <option> tag. it's illegal - it will really mess
things up for the browser and you may get inconsistent cross-browser
renderings. I think maybe what he was trying for was \n instead, which the
browser ignores, but looks good when viewing code.
$wynik=mysql_query($zapytanie);
if ($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC)) {
echo "<option value=$wiersz>$wiersz</option>\n";
} else {
echo "<!--no rows.-->";
}



Hilarion

Jan 30 '06 #8

"Leszek" <le*******@poczta.onet.pl> wrote in message
news:dp**********@news.onet.pl...
Hi.
How can I get just one row from selected column and put it into html
dropdown list
I tried like this:

function pobierz_wszystko($tabela,$kolumna)
you are referencing the array wrong. it should be $wiersz[$kolumna] when you
want to extract data from the column.
echo "<option value=$wiersz[$kolumna]>$wiersz[$kolumna]</option> <br
/>";

{
$zapytanie="SELECT $kolumna FROM $tabela";
$wynik=mysql_query($zapytanie);
while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz>$wiersz</option> <br />";
}
}

<select name="$hname">
<option value="*" selected >All</option>
<?php
pobierz_wszystko('hotel','hotel_nazwa');
?>
</select>
But it doesn't work properly because I'm getting a dropdown list with
All,and Array,Array,Array,Array,Array
Instead of Array I'd like to have a value from a row.

Is it because I'm using mysql_fetch_array($wynik,MYSQL_ASSOC)?
or maybe the query is wrong?

Thanks
Leszek

Jan 30 '06 #9

"Jim Michaels" <jm******@yahoo.com> wrote in message
news:sZ********************@comcast.com...

"Hilarion" <hi******@SPAM.op.SMIECI.pl> wrote in message
news:dp**********@news.onet.pl...
How can I get just one row from selected column and put it into html
dropdown list
I tried like this:

function pobierz_wszystko($tabela,$kolumna)
{
$zapytanie="SELECT $kolumna FROM $tabela";
$wynik=mysql_query($zapytanie);
while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz>$wiersz</option> <br />";
}
}

The query is wrong among other things.

proper: Select $kolumna from $tabela limit 1

You can also use an offset, reference the manual at
http://dev.mysql.com/doc/refman/4.1/en/select.html

Another thing is, if you are *always* going to want just one result, not
only should you use the proper select, but you should also limit your
code to only ask for 1 result:

$wynik=mysql_query($zapytanie);
$wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC)
echo "<option value=$wiersz>$wiersz</option> <br />";

notice the lack of using a "while" statement, which is not appropriate
for 1 result queries.

I think that Leszek wanted to ask "how can I get just one COLUMN
from...",
in which case LIMIT clause will not be what he looks for.
As others explained - Leszek used the PHP mysql functions output in
a wrong way, and it had nothing to do with SQL syntax.

OOPS! code fix. array referenced wrong. didn't catch this until a later
post. fixed below.


Actually, he wanted "just one row from a selected column". If he was
speaking about a *particular* row, I would suggest a WHERE clause (SELECT
$kolumna FROM $tablea WHERE id=5), or a counter+if approach.
I am not sure exactly what it is he is trying to do.. maybe trying to pick
out a row to put a SELECTED attribute on the <option> on or something?
(speak up!) $count=1;
while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC ))
{
echo "<option value=$wiersz[$kolumna]>$wiersz[$kolumna]</option> <br
/>";
if (4==count) {
do something here
}
$count++;
}
the problem with this is, how are you going to guarantee that there will
always be more than 4 rows, if this is what he is really asking?

And if he is wanting just the first row from a SELECT, yeah, a LIMIT 1
would be good to append on the statement. but I would suggest the
following code below in case you get no rows (you can drop the else part
if you want):
He should remove the <br /> tag out of the <select></select> area - it
should not be beside an <option> tag. it's illegal - it will really mess
things up for the browser and you may get inconsistent cross-browser
renderings. I think maybe what he was trying for was \n instead, which
the browser ignores, but looks good when viewing code. $wynik=mysql_query($zapytanie);
if ($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC)) {
echo "<option value=$wiersz[$kolumna]>$wiersz[$kolumna]</option>\n";
} else {
echo "<!--no rows.-->";
}


Hilarion


Jan 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alvey Sidecast | last post by:
This is probably embarrassingly simple, but I've been trawling through this ng for hours now and my brain hurts. I've got an unbound multi-column listbox (multi-select=none) whose rowsource is a...
1
by: Thomas Zimmermann | last post by:
I have a form with a subform in datasheet view. Now, I want to trigger a procedure (P1) each time the user selects an entire column (by clicking in the heading) in the subform. The procedure (P1) I...
0
by: melanieab | last post by:
Hi, I'm trying to re-select the currently selected row after a DataGrid sort. When a row is selected, I get the unique string xDate that's associated with that row. When the Column Header is...
0
by: Francois Verbeeck | last post by:
Dear UseNet readers, Does anyone have any idea on how to colorize selected checkbox in checkboxlist control ? I've quite a huge checkboxlist (approximatively one full screen) and, to improve...
1
by: vishnu | last post by:
Hi, I have a fully editable datagrid which has dropdownlist in two columns. Now i have to bind the dropdown in the second column based on the value selected in the dropdown in the firstcolumn. so...
2
by: shumaker | last post by:
I have a combobox that is very much like the one found in the RSS project here: http://msdn.microsoft.com/vstudio/express/visualCSharp/learning/ My projectNameComboBox basically is filled with a...
0
by: gcardozo | last post by:
Could anyone help me with the task of copying and pasting the values of changing cell references in excel using code for a macro?(macro to copy and paste values within a currently selected column). ...
2
by: cogitoergosum | last post by:
Hi, I have a table with four columns. By default, except for the second column (id="s01") all column values are protected. When a value is selected from second column, the third column is...
2
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi, I can return the rowindex using SelectedRow or SelectedIndex But, how do I get my code to return the cell or column index of a selected row in my gridview? thanx -- Regards,
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.