php-general Digest 16 Nov 2010 01:51:32 -0000 Issue 7038
Topics (messages 309494 through 309497):
Switch Question...
309494 by: Steve Staples
309495 by: Ashley Sheridan
309496 by: Steve Staples
Re: PHP loop to issue sql insert
309497 by: admin.buskirkgraphics.com
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin Message ---
Ok, dumb question, and i have tested, but I want to ensure that my tests
were accurate, and behavior is correct.
Ok, i have an integer, that based on what it is, does certain things...
switch ((int)$intval)
{
}
now, if i need to do something on a few of the numbers, then:
case 0:
# do nothing as strings will be (int) as 0
break;
case 1:
# do this
break;
case 12:
# do this
break;
default:
# do everything that is general to this
which this works, as the $intval should never be anything that i am not
expecting as there is another part of the code that decides which int's
to send to this class.
but is there a better way to do this? this could potentially do a lot
more, and be more dynamic, but there is also the possibilty that i need
to have more "custom" commands based on an integer value, and not
something that is "generic".
Is there a way to only do from like cases 2-11, without doing:
case 2:
case 3:
case 4:
.....
case 11:
# do this stuff
break;
am I just overthinking things? i dunno... it's monday... brains still
on weekend mode.
Steve
--- End Message ---
--- Begin Message ---
On Mon, 2010-11-15 at 16:27 -0500, Steve Staples wrote:
> Ok, dumb question, and i have tested, but I want to ensure that my tests
> were accurate, and behavior is correct.
>
> Ok, i have an integer, that based on what it is, does certain things...
>
> switch ((int)$intval)
> {
> }
>
> now, if i need to do something on a few of the numbers, then:
> case 0:
> # do nothing as strings will be (int) as 0
> break;
> case 1:
> # do this
> break;
> case 12:
> # do this
> break;
> default:
> # do everything that is general to this
>
> which this works, as the $intval should never be anything that i am not
> expecting as there is another part of the code that decides which int's
> to send to this class.
>
> but is there a better way to do this? this could potentially do a lot
> more, and be more dynamic, but there is also the possibilty that i need
> to have more "custom" commands based on an integer value, and not
> something that is "generic".
>
> Is there a way to only do from like cases 2-11, without doing:
> case 2:
> case 3:
> case 4:
> .....
> case 11:
> # do this stuff
> break;
>
>
> am I just overthinking things? i dunno... it's monday... brains still
> on weekend mode.
>
> Steve
>
>
There is actually a very cool way you can use a switch in PHP to do what
you want:
switch(true)
{
case ($intval >= 2 && $intval <= 11):
{
// stuff here for cases 2-11 inclusive
break;
}
}
Although I think in your case a series of if/else if statements would
work, and might possibly be more readable in your code.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--- End Message ---
--- Begin Message ---
On Mon, 2010-11-15 at 22:43 +0000, Ashley Sheridan wrote:
> On Mon, 2010-11-15 at 16:27 -0500, Steve Staples wrote:
>
> > Ok, dumb question, and i have tested, but I want to ensure that my tests
> > were accurate, and behavior is correct.
> >
> > Ok, i have an integer, that based on what it is, does certain things...
> >
> > switch ((int)$intval)
> > {
> > }
> >
> > now, if i need to do something on a few of the numbers, then:
> > case 0:
> > # do nothing as strings will be (int) as 0
> > break;
> > case 1:
> > # do this
> > break;
> > case 12:
> > # do this
> > break;
> > default:
> > # do everything that is general to this
> >
> > which this works, as the $intval should never be anything that i am not
> > expecting as there is another part of the code that decides which int's
> > to send to this class.
> >
> > but is there a better way to do this? this could potentially do a lot
> > more, and be more dynamic, but there is also the possibilty that i need
> > to have more "custom" commands based on an integer value, and not
> > something that is "generic".
> >
> > Is there a way to only do from like cases 2-11, without doing:
> > case 2:
> > case 3:
> > case 4:
> > .....
> > case 11:
> > # do this stuff
> > break;
> >
> >
> > am I just overthinking things? i dunno... it's monday... brains still
> > on weekend mode.
> >
> > Steve
> >
> >
>
>
> There is actually a very cool way you can use a switch in PHP to do what
> you want:
>
> switch(true)
> {
> case ($intval >= 2 && $intval <= 11):
> {
> // stuff here for cases 2-11 inclusive
> break;
> }
> }
>
> Although I think in your case a series of if/else if statements would
> work, and might possibly be more readable in your code.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
yeah, totally forgot about putting logic into the case... yep... still
monday here...
thanks ash!
--- End Message ---
--- Begin Message ---
Example:
$simple = "select count(*) as count, searchkeywords from searchtable group
by searchkeywords order by count desc";
$stuff = mysql_query($simple);
If(mysql_num_rows($stuff) >= 1)
{
While($toarray = mysql_fetch_assoc($stuff))
{
$doit = mysql_query("INSERT INTO mytable (count, color) values
('$toarray[count]', '$toarray[searchkeywords]')");
Echo mysql_error();
}
}
I am not sure why, don't care. Here is a very simple pull from this table
insert into this table with the looped data.
Richard L. Buskirk
-----Original Message-----
From: Simon J Welsh [mailto:si...@welsh.co.nz]
Sent: Sunday, November 14, 2010 8:16 PM
To: Rick Dwyer
Cc: php-gene...@lists.php.net
Subject: Re: [PHP] PHP loop to issue sql insert
On 15/11/2010, at 12:47 PM, Rick Dwyer wrote:
> Hello List.
>
> I have a sql command that counts, groups and sorts data from a table. I
need to insert the results of that sql command into different table.
>
> My sql SELECT looks like this:
>
> select count(*) as count, searchkeywords from searchtable group by
searchkeywords order by count desc;
>
> and returns records like this:
>
> 578 green
> 254 blue
> 253 red
> 253 yellow
> 118 orange
> .... etc.
>
> My PHP looks like this so far:
>
> $sql = "select count(*) as count, searchkeywords from searchtable group by
searchkeywords order by count desc";
> $result = @mysql_query($sql,$connection) or die("Couldn't execute checkcat
query");
> $num = mysql_num_rows($result);
> echo $num;
>
>
> The echo is of course returning the total number of records.... not data
as listed above.
>
> I know a WHILE statement is to be used here, but can't get it to work.
>
> How do I loop through the above found data, inserting each value as like
this:
>
> insert into mytable (count, color) values ("578", "green");
> insert into mytable (count, color) values ("254", "blue");
> ...etc
>
>
>
> Thanks,
>
>
> --Rick
Personally I'll get MySQL to do it for me using: insert into mytable (count,
color) select count(*) as count, searchkeywords from searchtable group by
searchkeywords order by count desc (see
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html for more
information on INSERT ... SELECT)
Otherwise, you'll want to use mysql_fetch_assoc($result). Examples and
information can be found at http://php.net/mysql_fetch_assoc
---
Simon Welsh
Admin of http://simon.geek.nz/
Who said Microsoft never created a bug-free program? The blue screen never,
ever crashes!
http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--- End Message ---