Thread: forms /selects
Hi all I am trying to do a select from a table after the user has choosen the first letter of the name and it must display only the names with that letter selected in a select box. The message i get is if i select a for example is: "the atribute 'a' not found" I would like some help to know what?s going wrong! Thank?s in advance Ângelo Rigo Here is my code: Search names by the first letter <BR><form method="post" size="1" action="<?php print("$PHP_SELF"); ?>"> <select size="1" name="leter"> <option selected>Choose an initial leter <option value=A>A <option value=B>B <option value=C>C <option value=D>D <option value=E>E <option value=F>F <option value=G>G <option value=H>H <option value=I>I <option value=J>J <option value=K>K <option value=L>L <option value=M>M <option value=N>N <option value=O>O <option value=P>P <option value=Q>Q <option value=R>R <option value=S>S <option value=T>T <option value=U>U <option value=V>V <option value=X>X <option value=Y>Y <option value=Z>Z </select><input type="submit" value="Submit"></form></TD> </TR> </TABLE> <br> <?php $db = pg_connect("dbname=db user=user"); $query ="SELECT name FROM thetable WHERE (nome=$leter%) ORDER BY nome ASC "; $result = pg_exec($db, $query); if (!$result) {printf ("Error!"); exit;} $numrows = pg_numrows($result); $row=0; printf ("<table border=0>"); printf ("<tr bgcolor='#FFFF33'><td><b>Name</b></td></tr>"); do { $myrow = pg_fetch_array ($result,$row); if($row % 2) { $bgcolor="#FFFF99"; } else { $bgcolor=""; } printf ("<tr bgcolor='$bgcolor'><td>%s</td></tr>",$myrow[nome]); $row++; } while ($row < $numrows); printf ("</table>"); pg_close($db); ?> ________________________________________ A busca mais veloz e precisa da internet. Acesse agora: http://www.zoom.com.br.
I see a couple of errors, one of which is the source of your problem.
You write:
"SELECT name FROM thetable WHERE (nome=$leter%) ORDER BY nome ASC";
This would evaluate to
"SELECT name FROM thetable WHERE (nome=A%) ORDER BY nome ASC";
What you need to write is:
"SELECT name FROM thetable WHERE (nome like '$leter%') ORDER BY nome ASC";
"like" is required to make regular expression match.
The quotes are always required when dealing with strings (and a few other types).
You could have just debugged this by
echo $query;
Also: Letter contains two t's. :)
Regards
/Jørn Cornelius Olsen
Hi I see my error was not using the "like" but now i fall in other problem: I have made an if else to display a message if have no name with the selected leter as initial it works but disply too: Warning: Unable to jump to row 0 on PostgreSQL result index 2 in /var/www/html/inscricao/resultado_a1.php on line 80 how could i handle this? Thank?s in advance: here is my actual code: Pesquisa de nome por letra inicial <BR><form method="post" size="1" action="<?php print("$PHP_SELF"); ?>"> <select size="1" name="letra"> <option selected>Escolha letra inicial <option value=A>A <option value=B>B <option value=C>C <option value=D>D <option value=E>E <option value=F>F <option value=G>G <option value=H>H <option value=I>I <option value=J>J <option value=K>K <option value=L>L <option value=M>M <option value=N>N <option value=O>O <option value=P>P <option value=Q>Q <option value=R>R <option value=S>S <option value=T>T <option value=U>U <option value=V>V <option value=X>X <option value=Y>Y <option value=Z>Z </select><input type="submit" value="Enviar"></form></TD> </TR> </TABLE> <br> <?php $db = pg_connect("dbname=db user=user"); $query ="SELECT name FROM aprovados WHERE (nome like '$leter%') ORDER BY name ASC "; $result = pg_exec($db, $query); if (!$result) {printf ("Não há nomes com esta letra inicial!"); exit;} $numrows = pg_numrows($result); /*********************** actual problem ***********************/ if ($numrows=='0') { print("Não foram encontrados nomes iniciados por : $letra"); print("<a href='javascript:history.back(-1);'><<Voltar</a>"); } else $row=0; printf ("<table border=0>"); printf ("<tr bgcolor='#FFFF33'><td><b>Nome</b></td></tr>"); do { $myrow = pg_fetch_array ($result,$row); if($row % 2) { $bgcolor="#FFFF99"; } else { $bgcolor=""; } printf ("<tr bgcolor='$bgcolor'><td>%s</td></tr>",$myrow[nome]); $row++; } while ($row < $numrows); printf ("</table>"); pg_close($db); ?> '>'-- Mensagem Original -- '>'To: pgsql-php@postgresql.org '>'Subject: Re: [PHP] forms /selects '>'From: jco@cornelius-olsen.dk '>'Date: Tue, 10 Dec 2002 15:03:33 +0100 '>' '>' '>'I see a couple of errors, one of which is the source of your problem. '>'You write: '>' "SELECT name FROM thetable WHERE (nome=$leter%) ORDER BY nome ASC"; '>'This would evaluate to '>' "SELECT name FROM thetable WHERE (nome=A%) ORDER BY nome ASC"; '>' '>'What you need to write is: '>' "SELECT name FROM thetable WHERE (nome like '$leter%') ORDER BY nome '>'ASC"; '>' '>'"like" is required to make regular expression match. '>'The quotes are always required when dealing with strings (and a few other '>' '>'types). '>' '>'You could have just debugged this by '>' echo $query; '>' '>'Also: Letter contains two t's. :) '>' '>'Regards '>'/Jørn Cornelius Olsen ________________________________________ A busca mais veloz e precisa da internet. Acesse agora: http://www.zoom.com.br.
Here is the final working code: <BR><form method="post" size="1" action="<?php print("$PHP_SELF"); ?>"> <select size="1" name="leter"> <option selected>Escolha letra inicial <option value=A>A <option value=B>B <option value=C>C <option value=D>D <option value=E>E <option value=F>F <option value=G>G <option value=H>H <option value=I>I <option value=J>J <option value=K>K <option value=L>L <option value=M>M <option value=N>N <option value=O>O <option value=P>P <option value=Q>Q <option value=R>R <option value=S>S <option value=T>T <option value=U>U <option value=V>V <option value=X>X <option value=Y>Y <option value=Z>Z </select><input type="submit" value="Enviar"> </form> </TD> </TR> </TABLE> <br> <?php $db = pg_connect("dbname=db user=user"); $query ="SELECT name FROM table WHERE (nome like '$leter%') ORDER BY name ASC "; $result = pg_exec($db, $query); if (!$result) {printf ("Não há nomes com esta letra inicial!"); exit;} $numrows = pg_numrows($result); if (!$numrows) { print("Não foram encontrados nomes iniciados por : $letra"); print("<a href='javascript:history.back(-1);'><<Voltar</a>"); } else{ $row=0; printf ("<table border=0>"); printf ("<tr bgcolor='#FFFF33'><td><b>Nome</b></td></tr>"); do { $myrow = pg_fetch_array ($result,$row); if($row % 2) { $bgcolor="#FFFF99"; } else { $bgcolor=""; } printf ("<tr bgcolor='$bgcolor'><td>%s</td></tr>",$myrow[name]); $row++; } while ($row < $numrows); printf ("</table>"); }//else pg_close($db); ?> '>'-- Mensagem Original -- '>'Date: Tue, 10 Dec 2002 13:52:45 -0300 '>'From: angelo.rigo@globo.com '>'Subject: Re: [PHP] forms /selects '>'To: pgsql-php@postgresql.org '>' '>' '>'Hi '>' '>'I see my error was not using the "like" '>'but now i fall in other problem: '>' '>'I have made an if else to display a message '>'if have no name with the selected leter as initial '>' '>'it works but disply too: '>' '>'Warning: Unable to jump to row 0 on PostgreSQL result index 2 in /var/www/html/inscricao/resultado_a1.php '>'on line 80 '>' '>'how could i handle this? '>' '>'Thank?s in advance: '>' '>'here is my actual code: '>' '>'Pesquisa de nome por letra inicial '>' <BR><form method="post" size="1" action="<?php print("$PHP_SELF"); ?>"> '>' <select size="1" name="letra"> '>' <option selected>Escolha letra inicial '>' <option value=A>A '>' <option value=B>B '>' <option value=C>C '>' <option value=D>D '>' <option value=E>E '>' <option value=F>F '>' <option value=G>G '>' <option value=H>H '>' <option value=I>I '>' <option value=J>J '>' <option value=K>K '>' <option value=L>L '>' <option value=M>M '>' <option value=N>N '>' <option value=O>O '>' <option value=P>P '>' <option value=Q>Q '>' <option value=R>R '>' <option value=S>S '>' <option value=T>T '>' <option value=U>U '>' <option value=V>V '>' <option value=X>X '>' <option value=Y>Y '>' <option value=Z>Z '>' </select><input type="submit" value="Enviar"></form></TD> '>' </TR> '>' </TABLE> <br> '>' <?php '>'$db = pg_connect("dbname=db user=user"); '>'$query ="SELECT name FROM aprovados WHERE (nome like '$leter%') ORDER BY '>'name ASC "; '>'$result = pg_exec($db, $query); '>' '>'if (!$result) {printf ("Não há nomes com esta letra inicial!"); exit;} '>'$numrows = pg_numrows($result); '>' '>' '>'/*********************** '>' actual problem '>'***********************/ '>' '>'if ($numrows=='0') { '>' print("Não foram encontrados nomes iniciados por : $letra"); '>' print("<a href='javascript:history.back(-1);'><<Voltar</a>"); '>'} '>'else '>' '>'$row=0; '>'printf ("<table border=0>"); '>'printf ("<tr bgcolor='#FFFF33'><td><b>Nome</b></td></tr>"); '>'do '>'{ '>'$myrow = pg_fetch_array ($result,$row); '>' if($row % 2) { '>' $bgcolor="#FFFF99"; '>' } '>' else { '>' $bgcolor=""; '>' } '>'printf ("<tr bgcolor='$bgcolor'><td>%s</td></tr>",$myrow[nome]); '>'$row++; '>'} '>'while ($row < $numrows); '>'printf ("</table>"); '>'pg_close($db); '>'?> '>' '>' '>' '>' '>' '>' '>' '>'-- Mensagem Original -- '>' '>'To: pgsql-php@postgresql.org '>' '>'Subject: Re: [PHP] forms /selects '>' '>'From: jco@cornelius-olsen.dk '>' '>'Date: Tue, 10 Dec 2002 15:03:33 +0100 '>' '>' '>' '>' '>' '>'I see a couple of errors, one of which is the source of your problem. '>' '>'You write: '>' '>' "SELECT name FROM thetable WHERE (nome=$leter%) ORDER BY nome '>'ASC"; '>' '>'This would evaluate to '>' '>' "SELECT name FROM thetable WHERE (nome=A%) ORDER BY nome ASC"; '>' '>' '>' '>'What you need to write is: '>' '>' "SELECT name FROM thetable WHERE (nome like '$leter%') ORDER '>'BY nome '>' '>'ASC"; '>' '>' '>' '>'"like" is required to make regular expression match. '>' '>'The quotes are always required when dealing with strings (and a few '>'other '>' '>' '>' '>'types). '>' '>' '>' '>'You could have just debugged this by '>' '>' echo $query; '>' '>' '>' '>'Also: Letter contains two t's. :) '>' '>' '>' '>'Regards '>' '>'/Jørn Cornelius Olsen '>' '>' '>'________________________________________ '>'A busca mais veloz e precisa da internet. Acesse agora: http://www.zoom.com.br. '>' '>' '>' '>'---------------------------(end of broadcast)--------------------------- '>'TIP 6: Have you searched our list archives? '>' '>'http://archives.postgresql.org ________________________________________ A busca mais veloz e precisa da internet. Acesse agora: http://www.zoom.com.br.