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IG9yYW5nIGxhaW4uDQpodHRwOi8vZGlj YXJ2ZS5ibG9nc3Bv
dC5jb20NCg==

Re: switch and while

am 28.08.2009 08:42:05 von dmagick

Emiliano Boragina wrote:
> Thanks Arie...
> But I need to do "category1" be the ID of my table.
> Using this way I have CASE number_ID_table echo string_CATEGORY_FIELD_table,
> and will be automatic... Many CASES as ID I having in the database

If the $selectCategory is part of the database query, then you need to
do it like that.

switch ($selectCategory)
{
case 'category1':
$query = .....;
break;

case 'category2':
$query = .....;
break;
}

You haven't really explained what you're trying to achieve so it's a bit
hard to suggest anything.

--
Postgresql & php tutorials
http://www.designmagick.com/


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: switch and while

am 28.08.2009 08:56:18 von Emiliano Boragina

Hi Chris,
Sorry... you're right!
With this I want to print the category in a html table after a search...
The search find the data, and print category and description. This =
category
is a number (ID), but I want to print the name of the category. My MYSQL
CATEGORY TABLE have ID and NAME, so using the SWITCH, I want to put in =
CASE
the ID and ECHO the NAME.
In other webs I do
switch($category) //where $category is an array fom the db
{
case 1:
echo "category1";
break;

case 2:
echo "category2";
break;

...
}
I want the 1, 2, 3... like the ID from my table, and "category1,
category2..." like the NAME from my table
Do not know if it was understood...
Thanks again

+ =
_
// Emiliano Boragina _
// Dise=F1o & Comunicaci=F3n //////////////////
+ =
_
// emiliano.boragina@gmail.com /
// 15 40 58 60 02 ///////////////////////////
+ =
_

-----Mensaje original-----
De: Chris [mailto:dmagick@gmail.com]=20
Enviado el: Viernes, 28 de Agosto de 2009 03:42 a.m.
Para: Emiliano Boragina
CC: 'Arie Nugraha'; php-db@lists.php.net
Asunto: Re: [PHP-DB] switch and while

Emiliano Boragina wrote:
> Thanks Arie...
> But I need to do "category1" be the ID of my table.
> Using this way I have CASE number_ID_table echo
string_CATEGORY_FIELD_table,
> and will be automatic... Many CASES as ID I having in the database

If the $selectCategory is part of the database query, then you need to=20
do it like that.

switch ($selectCategory)
{
case 'category1':
$query =3D .....;
break;

case 'category2':
$query =3D .....;
break;
}

You haven't really explained what you're trying to achieve so it's a bit =

hard to suggest anything.

--=20
Postgresql & php tutorials
http://www.designmagick.com/


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: switch and while

am 28.08.2009 11:43:17 von Emiliano Boragina

A retail...
I have two tables in my db.
One for product description, in this one I have the column CATEGORY as =
INT.
The other table is CATEGORY with ID and NAME of category.

So, I do a select from this two tables... but I need switch the number =
of
the CATEGORY in the first table to the NAME in the second table.

Thanks a lot

+ =
_
// Emiliano Boragina _
// Dise=F1o & Comunicaci=F3n //////////////////
+ =
_
// emiliano.boragina@gmail.com /
// 15 40 58 60 02 ///////////////////////////
+ =
_


-----Mensaje original-----
De: Chris [mailto:dmagick@gmail.com]=20
Enviado el: Viernes, 28 de Agosto de 2009 03:42 a.m.
Para: Emiliano Boragina
CC: 'Arie Nugraha'; php-db@lists.php.net
Asunto: Re: [PHP-DB] switch and while

Emiliano Boragina wrote:
> Thanks Arie...
> But I need to do "category1" be the ID of my table.
> Using this way I have CASE number_ID_table echo
string_CATEGORY_FIELD_table,
> and will be automatic... Many CASES as ID I having in the database

If the $selectCategory is part of the database query, then you need to=20
do it like that.

switch ($selectCategory)
{
case 'category1':
$query =3D .....;
break;

case 'category2':
$query =3D .....;
break;
}

You haven't really explained what you're trying to achieve so it's a bit =

hard to suggest anything.

--=20
Postgresql & php tutorials
http://www.designmagick.com/


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: switch and while

am 07.09.2009 02:51:58 von dmagick

Emiliano Boragina wrote:
> Hi Chris,
> Sorry... you're right!
> With this I want to print the category in a html table after a search...
> The search find the data, and print category and description. This category
> is a number (ID), but I want to print the name of the category. My MYSQL
> CATEGORY TABLE have ID and NAME, so using the SWITCH, I want to put in CASE
> the ID and ECHO the NAME.
> In other webs I do
> switch($category) //where $category is an array fom the db
> {
> case 1:
> echo "category1";
> break;
>
> case 2:
> echo "category2";
> break;
>
> ...
> }
> I want the 1, 2, 3... like the ID from my table, and "category1,
> category2..." like the NAME from my table
> Do not know if it was understood...

There are a couple of ways you can do it and no need for a switch.


1) get the category name as part of the query itself

This works nicely if an article only fits into one category. If it fits
into multiple categories, it gets a little messy (though it's still doable).


select * from articles where id='x';

becomes

select a.*, c.category_name from articles a inner join categories c on
(a.category_id=c.category_id) where id='x';


2) load all categories at once and then print the relevant one

This works if you have a small number of categories available. If you
have a lot (50+) then I'd look at the first method.

$category_list = array();
$category_query = "select * from categories order by category_name";
$category_result = mysql_query($category_query);
while ($row = mysql_fetch_assoc($category_result)) {
$cat_id = $row['category_id'];
$cat_name = $row['category_name'];
$category_list[$cat_id] = $cat_name;
}

then you can just print out the name, eg:

// article_category comes from your query to load the article.
$article_category = 'x';

// make sure it's a valid category name
if (isset($category_list[$article_category])) {
echo "Your article is in category " .
htmlspecialchars($category_list[$article_category]);
}

--
Postgresql & php tutorials
http://www.designmagick.com/


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php