Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Stephen
Feb 6, 2004

Stoned
Just to add further:
code:
$idCanine = 0;
$testdbo = new PDO('mysql:host='.$this->dbHost.';dbname='.$this->dbName.';', $this->dbUser, $this->dbPass);
if($idCanine == 0) {
	$stmt = $testdbo->prepare('CALL SP_S_CANINE_RANDOM()');
} else {
	$stmt = $testdbo->prepare('CALL SP_S_CANINE(:idCanine)');
	$stmt->bindParam(':idCanine', $idCanine);
}
$stmt->execute();
Declaring a new PDO and using that, instead of the class variable works properly as well.

I assume then, that there's something preventing PDO variables from being reused? Again, I still don't understand what/why.

Edit: Looks like I may have found the problem - http://bugs.php.net/bug.php?id=38861

Stephen fucked around with this message at 21:55 on Jul 11, 2008

Adbot
ADBOT LOVES YOU

Lankiveil
Feb 23, 2001

Forums Minimalist

drcru posted:

So I'm trying to implement Dijkstra's algorithm for a side project I'm working on but I'm having difficulty grasping how the whole thing works...

I've done this before but I don't have the code in front of me, so bear with me. But yes, I made the "sectors" or "room" the vertices and then had a separate table that acted as vertices/paths. The main drawback with my method was that one had to have the entire graph in memory, which was a bit of a downer when you only wanted to go across to the next node.

If you haven't got working code, this might help: http://en.giswiki.net/wiki/Dijkstra's_algorithm

LightI3ulb
Oct 28, 2006

Standard pleasure model.
I've got a bit of an issue with setting up a mail server with multiple levels (Squirrelmail, Postfix, Dovecot). Right now, I have a single login page that redirects to the proper site based on the "level" set in MySQL. The problem is that after the user is logged in, all they have to do is change the URL to access any other level.

I'm passing the username and password in a hidden POST to Squirrelmail, and I think the issue is that it's retaining the POST variables. Is there any way to clear them?

Duck and Cover
Apr 6, 2007

I was transferring working code to another server and presto it decided that it no longer wants to work. Here it is working NWS (no bad pictures but those words might get you) http://www.willworkforpie.com/theproject/index.php NWS and here it is breaking NWS (same reason as before) http://www.peoplepersonporn.com/ NWS

As you can see if you go to the broken website it spews out Warning: Cannot modify header information - headers already sent by (output started at /f1/content/peopleperson/public/index.php:5) in /f1/content/peopleperson/public/index.php on line 103 (bottom of the code, setcookie...)

How can I fix it while still ensuring the cookies get set? Oh and if you see anything horribly inefficient please do tell me.

code:
<?php include 'ads.php';
include 'connect.php';
banner();
$varform = $_GET['downVoteLink'];
//$ip=$_SERVER['REMOTE_ADDR'];
//echo "<font face='Verdana' size='3'><b>IP Address= $ip</b>"; 

function downVote($linkId) {
	  if($_COOKIE[$_GET['downVoteLink']] ==1 and $_COOKIE["cookieworks"] ==1) {
	   mysql_query("UPDATE plinko_test.table SET votes=votes-1 WHERE id = $linkId;");
	   setcookie($_GET['downVoteLink'], 0, time()+3600);
	  }
}


// -r------------ Connect to database and then table   ---------

 
mysql_connect ("$server", "$user", "$password" ); 
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query( "SELECT id,description, website,date FROM plinko_test.table WHERE votes>0  ORDER BY id desc") 
or die("SELECT Error: ".mysql_error()); 



// -------------    ---------

if($_GET["downVoteLink"] >= 0) {//-------- Checks to make sure it
	downVote($_GET["downVoteLink"]);
	
	
}



$num_rows = mysql_num_rows($result); 

while ($get_info = mysql_fetch_row($result)){ 

	// get_info[0] description
	// get_info[1] link address 
	// get_info[2] date
	// get_info[3] elimination variables
	// max text 150 varchar
	// prints the row, then goes onto next row
	//UPDATE the_table SET the_field = the_field + 1
	//WHERE [your WHERE clause] 
	//onclick=\"this.disabled=true;\"

	// pass the where to the top, which will increment the votes
	//
	// "<div id='test'><a href=\"$get_info[2]\" target='_blank'>$get_info[1]</a></div>";
	print "<table width='730'>
	<tr>
	
	<td width='50'><form action=\"test.php\" method=\"get\"><div id='text'>$get_info[3]</td><td width'641'><a href=\"$get_info[2]\" target='_blank' class=\"links\">$get_info[1]</a></div>
	</td>
	<td width='89'><div id='button'> <button type=\"submit\" name=\"downVoteLink\" value=\"$get_info[0]\">
	Link is Trash</button></div></form></td>
	</tr>";
	
	print "<div id='white'></div>";
	if(!isset($_COOKIE["$get_info[0]"])) {  // sets cookie if a cookie does not already exist
	setcookie("$get_info[0]", 1, time()+3600);
	}
}
mysql_close();?> 
</html>

Evil Angry Cat
Nov 20, 2004

Put ob_start(); as the first line of the page and ob_end_flush(); as the last one. Should work.

Duck and Cover
Apr 6, 2007

I had tried it before and it didn't work, I tried it again and.... still didn't work =/.


EDIT I'm an idiot, it works thank you.

Duck and Cover fucked around with this message at 06:40 on Jul 15, 2008

chocojosh
Jun 9, 2007

D00D.

LightI3ulb posted:

I've got a bit of an issue with setting up a mail server with multiple levels (Squirrelmail, Postfix, Dovecot). Right now, I have a single login page that redirects to the proper site based on the "level" set in MySQL. The problem is that after the user is logged in, all they have to do is change the URL to access any other level.

I'm passing the username and password in a hidden POST to Squirrelmail, and I think the issue is that it's retaining the POST variables. Is there any way to clear them?

Why not just use a LoggedIn session variable?

Stephen
Feb 6, 2004

Stoned
How would I check in PHP if a string exists as a variable? isset will only tell me if a variable exists, and does not accept a string as a parameter.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Stephen posted:

How would I check in PHP if a string exists as a variable? isset will only tell me if a variable exists, and does not accept a string as a parameter.

php:
<?
$var = "hello";

$varName = "var";

if (isset($$varName)) //this is like writing if (isset($var))
echo $var;
?>
I think the double $$ is what you are looking for.

jasonbar
Apr 30, 2005
Apr 29, 2005

Stephen posted:

How would I check in PHP if a string exists as a variable? isset will only tell me if a variable exists, and does not accept a string as a parameter.

php:
<?php
$foo "bar";
$baz "foo"// the name of the variable you want to check.

if (isset($$baz))
  ...
?>

Using a double $$ evaluates the value as a variable. Is there a more correct way to do this? I dunno.

e: f;b

waffle iron
Jan 16, 2004

Stephen posted:

How would I check in PHP if a string exists as a variable? isset will only tell me if a variable exists, and does not accept a string as a parameter.
While the solutions above are right, it makes me wonder if the real problem isn't the architecture of the code. If you're using register globals that's probably a bad idea. If you just need arbitrary data storage with string names, a better idea would be to use associative arrays (i.e. $data["foo"]).

Stephen
Feb 6, 2004

Stoned

waffle iron posted:

While the solutions above are right, it makes me wonder if the real problem isn't the architecture of the code. If you're using register globals that's probably a bad idea. If you just need arbitrary data storage with string names, a better idea would be to use associative arrays (i.e. $data["foo"]).
I was trying to write a method that I could use to pre-populate form fields based on what the field name was.
Sort of what CakePHP does with their html helper.
e.g.
code:
echo text($fieldname, $attributes[]);

function $text($fieldname, $attributes) {
   //determine if $fieldname has an entry in the POST array, or if it exists as a variable
   return '<input type="text" value="//variable or POST value//">';
}

Duck and Cover
Apr 6, 2007

Evil Angry Cat posted:

Put ob_start(); as the first line of the page and ob_end_flush(); as the last one. Should work.

I apparently did not read carefully. First line of the page means just that before everything, even the html etc. I in my brilliant fashion decided it meant the first line of php code.

Thank you it works now.

Duck and Cover fucked around with this message at 06:40 on Jul 15, 2008

Safety Shaun
Oct 20, 2004
the INTERNET!!!1
I've been browsing and I can't make sense of how to get my CSV file into an array so I can begin working with the data in the script.

The file is uploaded fine and the location of the file is in $file, I then want something like $Col1[0] $Col2[0] and $Col3[0] respectively with the 200 or so rows that are in the file.

jasonbar
Apr 30, 2005
Apr 29, 2005

Safety Shaun posted:

I've been browsing and I can't make sense of how to get my CSV file into an array so I can begin working with the data in the script.

The file is uploaded fine and the location of the file is in $file, I then want something like $Col1[0] $Col2[0] and $Col3[0] respectively with the 200 or so rows that are in the file.

http://us3.php.net/fgetcsv should do the trick.

php:
<?php
$fh fopen($file"r");
$data = array();
$titles fgetcsv($fh1000','); // you can skip this to have the column
                                   // titles be $data[0]

while (($data[] = fgetcsv($fh1000',')) !== FALSE)
{
   ...
}

fclose($fh);
?>

jasonbar fucked around with this message at 01:51 on Jul 15, 2008

Safety Shaun
Oct 20, 2004
the INTERNET!!!1

jasonbar posted:

http://us3.php.net/fgetcsv should do the trick.

Excellent!

Now I have this output:
php:
<?
$row = 1;
$handle = fopen($loc, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
    }
}
fclose($handle);
?>
php:
<?
 3 fields in line 1:
STRING 1
STRING 2
STRING 3

3 fields in line 2:
STRING A
STRING B
STRING C
?>
How do I say, echo out only "STRING 3"

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Safety Shaun posted:

Excellent!

How do I say, echo out only "STRING 3"

Your first question could have been answered by googling "php csv" (fgetcsv is the FIRST RESULT). Now you have copied and pasted the example code from that page and asked another extremely basic question that you should be able to figure out on your own. Either pay somebody to write this script for you or actually take the time to learn how to write it.

Safety Shaun
Oct 20, 2004
the INTERNET!!!1

fletcher posted:

Your first question could have been answered by googling "php csv" (fgetcsv is the FIRST RESULT). Now you have copied and pasted the example code from that page and asked another extremely basic question that you should be able to figure out on your own. Either pay somebody to write this script for you or actually take the time to learn how to write it.
I'm learning. So gently caress off jerkbag.

This is a place for people to ask for help and some people are very helpful.

Vanadium
Jan 8, 2005

Safety Shaun posted:

This is a place for people to ask for help and some people are very helpful.

In the long run, people making you learn on your own are more helpful than those doing your homework. :unsmith:

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

Safety Shaun posted:

I'm learning. So gently caress off jerkbag.

This is a place for people to ask for help and some people are very helpful.

Come on, man. You're not even trying. Your previous post seems to indicate that you know how an array works, so how can you not be able to figure out what you want to do from the example you copied?

Here's a hint: $data is an array of the columns.

Safety Shaun
Oct 20, 2004
the INTERNET!!!1

DaTroof posted:

Come on, man. You're not even trying. Your previous post seems to indicate that you know how an array works, so how can you not be able to figure out what you want to do from the example you copied?

Here's a hint: $data is an array of the columns.

Thank you. I'm not sure if this was the easiest way to go about it but it works for me:
php:
<?
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    
    $row++;
    
    for ($c=0; $c < $num; $c++) {
        
    $entry[$row-1][$c+1] = $data[$c];   //this line
    }
}
?>
So I can call entry[1][2] for row 1, column 2.

nbv4
Aug 21, 2002

by Duchess Gummybuns
I'm trying to crawl a webpage using file_get_contents(), but it is giving me a 403 error. If I copy the exact same URL that the function is betting to a webbrowser, the page loads fine, but in PHP I get this error:

Warning: file_get_contents(http://gc.kls2.com/airport/HECA) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /var/www/logbook/classes/map_class.php on line 252

I assume the site owner has some anti-spam thing in place which is why I'm blocked. Is there any way around this? This particular function is only going to be ran once or twice a month, so its not like I'm going to be overloading the site's bandwidth...

nbv4 fucked around with this message at 06:38 on Jul 16, 2008

jasonbar
Apr 30, 2005
Apr 29, 2005

nbv4 posted:

I'm trying to crawl a webpage using file_get_contents(), but it is giving me a 403 error. If I copy the exact same URL that the function is betting to a webbrowser, the page loads fine, but in PHP I get this error:

Warning: file_get_contents(http://gc.kls2.com/airport/HECA) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /var/www/logbook/classes/map_class.php on line 252

I assume the site owner has some anti-spam thing in place which is why I'm blocked. Is there any way around this? This particular function is only going to be ran once or twice a month, so its not like I'm going to be overloading the site's bandwidth...

They are clearly trying to block what you are attempting. However, to answer your question:
php:
<?php
// Use any useragent string you want.
ini_set('user_agent''Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0');

var_dump(file_get_contents('http://gc.kls2.com/airport/HECA'));

?>

This worked for me - please only use it for good.

nbv4
Aug 21, 2002

by Duchess Gummybuns

jasonbar posted:

This worked for me - please only use it for good.

sweeeet, that worked. I won't be bad I promise. Bandwidth usage from this script will probably amount to a few hundred KB's per month.

dagard
Mar 31, 2005
Is there any good reason why implode() has to take an array? IE, why this:

code:
$whatever = implode("|", "A", "B", "C", "D");
won't work, but put #s 2-5 inside of array(), it's fine? Just backwards compatibility?

Seriously, I want to know if I missed some blatantly obvious reason. I'm a Perl guy (yeah, I know, burn the heretic), not a PHP guy, I just have to deal with it a lot at work.

Zorilla
Mar 23, 2005

GOING APE SPIT

dagard posted:

Is there any good reason why implode() has to take an array? IE, why this:

Take a look at its syntax here. It expects exactly two parameters as input. Parameter one is the glue string (such as a comma) and parameter two is a set of strings you wish to implode.

I think it's a good way of doing things because it allows future versions of PHP to expand functionality by possibly allowing extra parameters after the second one.

Zorilla fucked around with this message at 12:25 on Jul 16, 2008

beerinator
Feb 21, 2003

dagard posted:

Is there any good reason why implode() has to take an array? IE, why this:

code:
$whatever = implode("|", "A", "B", "C", "D");
won't work, but put #s 2-5 inside of array(), it's fine? Just backwards compatibility?

The reason that won't work is that in your example you are giving implode 5 parameters when it is expecting two.

Div
May 27, 2001

dagard posted:

Is there any good reason why implode() has to take an array? IE, why this:

code:
$whatever = implode("|", "A", "B", "C", "D");
won't work, but put #s 2-5 inside of array(), it's fine? Just backwards compatibility?

Seriously, I want to know if I missed some blatantly obvious reason. I'm a Perl guy (yeah, I know, burn the heretic), not a PHP guy, I just have to deal with it a lot at work.
It's pretty obvious, the way passing parameters and using them in functions is completely different in Perl and PHP. I'd agree it's much better in Perl though.

func_get_args() has to be called explicitly to do it "the Perl way".

php:
<?
function perl_implode()
{
    $args = func_get_args();
    $glue = array_shift($args);

    return implode($glue, $args);
}

perl_implode('|', 'A', 'B', 'C', 'D', etc..);
?>

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Does anyone have any idea on how to efficiently create a random maze via PHP?

I found this one but I'm not quite sure on how to make it better. It takes quite awhile for it to make 20x20 maze as it seems to make them by going through every cell in the grid.

Anyone happen to know the name of the formula that makes up a maze?

jasonbar
Apr 30, 2005
Apr 29, 2005

drcru posted:

Does anyone have any idea on how to efficiently create a random maze via PHP?

I found this one but I'm not quite sure on how to make it better. It takes quite awhile for it to make 20x20 maze as it seems to make them by going through every cell in the grid.

Anyone happen to know the name of the formula that makes up a maze?

Google says: http://en.wikipedia.org/wiki/Maze_generation_algorithm. Seems like Kruskal or Prim will be your friends.

Scaevolus
Apr 16, 2007

drcru posted:

Does anyone have any idea on how to efficiently create a random maze via PHP?

I found this one but I'm not quite sure on how to make it better. It takes quite awhile for it to make 20x20 maze as it seems to make them by going through every cell in the grid.

Anyone happen to know the name of the formula that makes up a maze?

A much faster maze generation algorithm is Eller's algorithm. The mazes might not be quite as pretty or have as nice properties, but it is perfect, and it's easy to do efficiently-- I used it to do maze generation in TI-89 BASIC, using only a few single-dimensional arrays and integers.

The idea is that you generate and output the maze one line at a time-- there's a good summary at http://www.astrolog.org/labyrnth/algrithm.htm (Ctrl-F Eller)



The mazes are pretty good if you tweak it properly.

Scaevolus fucked around with this message at 08:49 on Jul 18, 2008

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
How do I pause a PHP script? I've played with the sleep(); function, but it seems to 'lock' the rendering of the page until the sleep timer is up.

I want to set variables -> render a web page -> *wait 2 minutes* -> then execute code using the variables set earlier.

Is this possible?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Treytor posted:

How do I pause a PHP script? I've played with the sleep(); function, but it seems to 'lock' the rendering of the page until the sleep timer is up.

I want to set variables -> render a web page -> *wait 2 minutes* -> then execute code using the variables set earlier.

Is this possible?

After you render the page, insert a new row into a table with a timestamp and whatever variables you need for the script.

Setup a cron job to run ever minute or something, and it just queries that table and looks for any jobs it needs to complete. They're not directly connected, but one sets up the other.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Couldn't you use fork and have the child process sleep for 2 minutes?

jasonbar
Apr 30, 2005
Apr 29, 2005
edit: nvm
edit: not nevermind
call flush() right before you call sleep() - although if you are doing any onload stuff, it won't ever happen until 2 minutes later.
php:
<?php
 ...
flush();
sleep($x);
 ...
?>

I'm also wondering what you are trying to accomplish. I can't seem to come up with a reason that I would want to do this.

jasonbar fucked around with this message at 22:57 on Jul 18, 2008

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Avenging Dentist posted:

Couldn't you use fork and have the child process sleep for 2 minutes?

What about when the server crashes, how do you know which ones were sleeping and haven't been completed yet? (if that is a concern of his)

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Thanks again for your feedback, guys.

I don't really understand that fork thing, but am willing to give it a shot (I'm very new to PHP). This application is not mission critical, so worrying about server crashes is minimal.

I've also tried the flush(); thing, but that's still preventing the page from loading.

Here is what I have:

code:
<?
// php code, set vars and other super awesome stuff
flush();
?>

<HTML>
I want this to load immediately
</HTML>
<?
sleep(120);
// other PHP code, notably a form post sent out, which I want to happen 2 minutes after page load.
?>
Once I get this working, I'll share what it is I am doing exactly.

EDIT: I don't have terminal access or to a cron job, as this is fairly basic web hosting. A cron wouldn't be frequest enough, anyway as I want this to run exactly 2 minutes after page load. With a cron, there could be a minute delay if they page load right after the last cron goes through.

Treytor fucked around with this message at 00:27 on Jul 19, 2008

jasonbar
Apr 30, 2005
Apr 29, 2005

Treytor posted:

I've also tried the flush(); thing, but that's still preventing the page from loading.

No sir, flush AFTER the html, right BEFORE the sleep(). Here is a working example: http://burntmail.net/flush.php

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!

jasonbar posted:

No sir, flush AFTER the html, right BEFORE the sleep(). Here is a working example: http://burntmail.net/flush.php

Would PHP being in safe mode interfere with this at all? Your script works perfectly on your page (obviously), but if I copy that code exactly and run it on mine, the page doesn't load until after sleep is done.

Adbot
ADBOT LOVES YOU

jasonbar
Apr 30, 2005
Apr 29, 2005

Treytor posted:

Would PHP being in safe mode interfere with this at all? Your script works perfectly on your page (obviously), but if I copy that code exactly and run it on mine, the page doesn't load until after sleep is done.

mod_gzip being enabled will result in that not working. Others have had mixed success using a combination of ob_start(), ob_end_flush() and flush(). You can try:

php:
<?php
// your html
ob_end_flush(); // note that you need not call ob_start()
flush();
sleep(5);
?>

If you have use of .htaccess files, you can try:
code:
<IfModule mod_gzip.c>
mod_gzip_on no
</IfModule>
Failing that, http://us.php.net/flush may provide additional options and information.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply