Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL
Author Message
Dawg
RavenNuke(tm) Development Team



Joined: Nov 07, 2003
Posts: 928

PostPosted: Tue Nov 25, 2008 7:26 am Reply with quote

I have alist of stations...
station1,station2,station3 and so on....

I need to loop this array through a call to the databse....

Code:
$querystr = "SELECT title,description FROM ".$prefix."_xtable WHERE title='station1'";

      $resultstations = sql_query($querystr, $dbi)
      or die ("Invalid query in Title Results");

while($row = mysql_fetch_array($resultstations))
  {
  $source1=$row['title'] . "<br />" . $row['description'];
  }



and bring them out to the $sourceX var.

So it looks up station1 in the database and returns description. then does the next one in order....and this is where I lose it...staion2 needs to come back as $source2. Station3 needs to come back as $source3 and so on.

I just can not see it in my head.

No need to write the code for me....just explain how I get to it?

I could write them as 16 MYSQL statements...but that does not seem like the right way to do it. I would think making 1 MYSQL call....then match Station1 to Source1, Station2 to Source2....??

Thank You for your help!!

Dawg
 
View user's profile Send private message
fkelly
Former Moderator in Good Standing



Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY

PostPosted: Tue Nov 25, 2008 9:42 am Reply with quote

Just quickly, I haven't tested this but:

1. Get rid of the dbi. Look throughout the code for RN and you will see syntax like:
Code:
$result = $db->sql_query('SELECT * FROM ' . $user_prefix . '_users_fields WHERE (need = \'2\') OR (need = \'3\') ORDER BY pos');

   while ($sqlvalue = $db->sql_fetchrow($result)) {

} // end of while loop

I just stole the code from YA that I had open but that's how it should be done.

Then in your loop, if you want separately named variables (an array might be preferable but if you do) then do something like this:

1. before the while loop make $i=0;
2. in the loop use the $ $ variable construct something like
i++;
$source = 'source' . $i;
$ $source = row['title'] . "<br />" . $row['description']

Read up on the $ $ construct if you are unfamiliar with it. It comes in real handy. (sorry forum doesn't like the two $ signs right next to each other and transforms them into smiley's ... but you need to put them together in your code).
 
View user's profile Send private message Visit poster's website
Guardian2003
Site Admin



Joined: Aug 28, 2003
Posts: 6799
Location: Ha Noi, Viet Nam

PostPosted: Tue Nov 25, 2008 10:45 am Reply with quote

Here's my contribution for the code you posted to convert to $db
Code:


global $db, $prefix;
$querystr = "SELECT title,description FROM ".$prefix."_xtable WHERE title='station1'";
      $resultstations = $db->sql_query($querystr)
      or die ("Invalid query in Title Results");

while($row = mysql_fetch_array($resultstations))
  {
  $source1=$row['title'] . "<br />" . $row['description'];
  }
 
View user's profile Send private message Send e-mail
djmaze
Subject Matter Expert



Joined: May 15, 2004
Posts: 727
Location: http://tinyurl.com/5z8dmv

PostPosted: Tue Nov 25, 2008 11:02 am Reply with quote

Code:
$i = 0;

while (++$i && $row)
{
    $s = 'source'.$i;
    $$s = $row;
}

_________________
$ mount /dev/spoon /eat/fun auto,overclock 0 1
ERROR: there is no spoon [ Only registered users can see links on this board! Get registered or login! ] 
View user's profile Send private message Visit poster's website
Dawg







PostPosted: Tue Nov 25, 2008 11:07 am Reply with quote

SWEET...Thank YOU!

I just came in from lunch and will go bang this thing out.

Dawg
 
Dawg







PostPosted: Tue Nov 25, 2008 2:56 pm Reply with quote

OK....So How BAD is this.....

It seems to be working just fine...but I know my methods need some work.

I Googled PHP and $ $ (Together $) and got nothing. So I wrote this....

Code:


$name = array(site1,site2,site3,site4);
$x=0;
foreach ($name as $value) {
$querystr = "SELECT title,description FROM ".$prefix."_xtable WHERE title='$value'";
      $result = $db->sql_query($querystr)
      or die ("Invalid query in Title Results");

while($row = mysql_fetch_array($result))
  {

$source[$x]= $row['title']. "<br />". $row['description'];

   $x++;
}
}


Teach me right and I will do it right!!

Thank You for your Time and effort!

Dawg
 
djmaze







PostPosted: Tue Nov 25, 2008 4:19 pm Reply with quote

Say you need update a variable but dynamically like $_GET does with register_globals you can use kaching!.
For example register_globals is turned off (which is a GOOD thing) but you have old code and don't have time to fix it (like the BAD code in nuke) then:

Code:
foreach ($_GET as $key => $value) { $$key = $value; }

It will create all the crap like $file (if there's a $_GET['file'])

You could do the same with
Code:
foreach ($_GET as $key => $value) { $GLOBALS[$key] = $value; }


It's actually bad if you have no idea which variables will be assigned (even worse when they are global)!

There's one reason it could be usefull, say you pre-defined variables and "maybe" overwrite them.
Code:


$site1 = false;
$site2 = false;
$site3 = false;
$site4 = false;
$result = query(SELECT site, title FROM table);
while ($row = $result->fetch_row())
{
    $$row[0] = $row[1];
}
 
Dawg







PostPosted: Tue Nov 25, 2008 4:45 pm Reply with quote

djmaze, Thank you for your input.

Site1 and so on are not $vars they are hard coded station names. What I was trying to do is make it so all teh $vars were set....and then overwritten....did I miss one?

Do I need to set $value? It is not set till it comes from the dbase....so I did not think I needed to set that one.

$source....I don;t know how I would set it. The idea was for it to come out and roll through source and set it to the output...hmmmm....will have to think about that.

Thank You again for your time!!

Dawg
 
fkelly







PostPosted: Tue Nov 25, 2008 7:09 pm Reply with quote

Look in the PHP manual under variable variables. I could quote it here but that would just repeat what's explained pretty well in the manual.

If I am looking for what is in the $_POST array for instance I will often (in diagnostic mode of course) write code like this:

Code:
/*foreach($_POST as $key => $value) {

   $a = $key;
   $aa = htmlentities($value);
   echo $a . ' ' . kaching!a . '<br />';
}


This will echo out to the screen anything that's being posted in from a preceding form so I can tell exactly what is getting set and what it's value is. So if I have a form field called "username" for instance $a will be "username" and $ $s will be whatever the user has put into the username field on the form (and that value will be stuffed into the variable $username.
 
djmaze







PostPosted: Wed Nov 26, 2008 11:17 am Reply with quote

@fkelly: see var_dump and print_r they quicker then your foreach
 
fkelly







PostPosted: Wed Nov 26, 2008 11:42 am Reply with quote

Thanks DJ. I will look those up and learn something. However, I just use that foreach for diagnostic purposes during development and testing ... so performance is not really an issue. It's getting a little off topic, but one thing I am trying to do with that is to make sure that everything I am trying to POST with a form gets posted (that can be a bit tricky with checkboxes especially) and that ONLY those things I am trying to POST in a form gets posted (to avoid a hacked form) and also that everything that gets POSTED gets validated appropriately (i.e., numeric fields are in fact numeric, fields that shouldn't have html in them don't, maxlengths aren't exceeded and the like). In conjunction with running the W3c validator against your form, running the foreach loop in the early development stages for a complex form can provide pretty good assurance that your form is working as intended.
 
Dawg







PostPosted: Wed Nov 26, 2008 5:55 pm Reply with quote

Thank Ya'll very much for your help!!

My app works just fine and does what it is supposed to do...until...one of the sites come back with a blank. So for the last couple of days I have been trying to figure out how to do the error checking and have gotten NO where!

So here goes....
I have an array. In this array is a list of Sites. I take that list of sites and look each one up inj the database and return the conditions. If any of the sites are NOT in the database...I need to return "No Data" for that site. I have tried and/if/or and but...(LOL!)....with no luck.

Once agian. No need to write the code for me....just a little theory help or a point in the right direction. I learn really good by examples if you know of one.

Here is my existing code....

Code:
$name = array(site1,site2,site3,site4);

$x=0;

foreach ($name as $value)
{
$querystr = "SELECT title,description FROM ".$prefix."_table1 WHERE title='$value'";
      $result = $db->sql_query($querystr)
      or die ("Invalid query in Title Results");

while($row = mysql_fetch_array($result))
  {
$source[$x]= $row['title']. "<br />". $row['description'];
echo $source[$x];
echo "<br />";
    $x++;     
   }
}


I need $source[$x] to come out with the missing site and "No data".

Note...I think....I have the db connection correct now. Thank You for pointing that out!


Thank You in advance for your time!

Oh'how I WISH I could hear the music!

Dawg

PS...
I have PMed everyone a link to what I am building. Just thought you might like to see what you were helping me with.
 
fkelly







PostPosted: Thu Nov 27, 2008 8:16 am Reply with quote

Maybe this is too simplistic but just do a numrows on your query first. Here's the syntax from a theme file I have open by chance:

Code:
        $numrows = $db->sql_numrows($db->sql_query('SELECT * FROM '.$prefix.'_banner WHERE active=\'1\''));



Just adapt that to your SQL. Do a numrows before your current $querystr inside the foreach loop. If $numrows == 0 then put source[$x] = 'no data' or something like that. If $numrows > 0 then do your current while loop. Increment $x inside the foreach loop instead of inside the while loop.

[/code]
 
Dawg







PostPosted: Thu Nov 27, 2008 12:32 pm Reply with quote

WOOT WOOT!

It is done!

Thank Ya'll for youyr help!

fKelly, That was just enough to push me over the top! Thank you!

That code will be reused a TON of times!

Stop by an check out the finished script.

I also have it in a center block on the front page.

Dawg
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©