|
Can I give a function multiple names? I have a function called distance_between(). It calculates the Euclidean distance between two points. At some point I might like to add functions that calculate the distance between two points according to other metrics. So I'd like to keep a function called distance_between() and have it calculate the Euclidean distance, but also have a function called euclidean_distance_between() that does the same thing, and have other functions like taxicab_distance_between() or whatever. Can I set it up so that distance_between() and euclidean_distance_between() are the same function? I know what the first thought you have is: just define distance_between() like the following: code:
|
# ? Apr 22, 2010 20:34 |
|
|
# ? Jun 6, 2024 03:20 |
Hammerite posted:But my function takes a variable number of arguments, so I don't know how I could make that work. (It takes anywhere from one to four arguments - each point specified can be given either as an object in my point class (thus as one argument), or as two numbers (thus as two consecutive arguments).) You can do a variable number of arguments in php. Maybe pass in something like DISTANCE::EUCLIDEAN into your distance_between function? That way you only have to talk to that same function every time.
|
|
# ? Apr 22, 2010 20:45 |
|
fletcher posted:You can do a variable number of arguments in php. I know - I've got the function working how I want it using func_num_args() and so on. It's just that I can't work out how I'd get a function to call another function and pass it the same arguments, given that I don't know how many arguments my "outside" function is going to be called with. fletcher posted:Maybe pass in something like DISTANCE::EUCLIDEAN into your distance_between function? That way you only have to talk to that same function every time. Maybe this is what I'll have to do.
|
# ? Apr 22, 2010 20:52 |
|
Maybe you're looking for func_get_args() and call_user_func_array()?php:<? function a(){ $args=func_get_args(); call_user_func_array('b',$args); } ?>
|
# ? Apr 22, 2010 21:44 |
|
barbarianbob posted:Maybe you're looking for func_get_args() and call_user_func_array()? Aha, so I write code:
|
# ? Apr 22, 2010 21:48 |
|
Is there a way to grab information from computers on your network such as IP address, MAC address, hostname, computer model, service tag, CPU and memory and maybe even hard drive usage with PHP on a linux server?
|
# ? Apr 23, 2010 00:38 |
IT Guy posted:Is there a way to grab information from computers on your network such as IP address, MAC address, hostname, computer model, service tag, CPU and memory and maybe even hard drive usage with PHP on a linux server? There was a really cool program I used like 5 years ago to do this...I can't remember the name of it though. Googling around I think it may have been Spiceworks. It would scan the network and find out all sorts of neat information about the computers on the network.
|
|
# ? Apr 23, 2010 01:10 |
|
fletcher posted:There was a really cool program I used like 5 years ago to do this...I can't remember the name of it though. Googling around I think it may have been Spiceworks. It would scan the network and find out all sorts of neat information about the computers on the network. You're correct, Spiceworks will do this and I have a server setup with Spiceworks. Although, I'm actually looking to hand code something into a customized intranet site/application.
|
# ? Apr 23, 2010 01:15 |
|
Open Audit is another one. I've used it and it is pretty slick. All you need is a web server.
|
# ? Apr 23, 2010 03:00 |
|
So I'm a noob PHP user who had to learn the language for my senior project in college. I have no professional training and no questions, just answers (I might post questions at a later date). So I was working with a script that prepares .txt files for mySQL inserts. I had used the foreach function to search through the arrays for useful data and came upon an interesting problem: I would have 300 lines to start and end up with 16k lines in my .txt file. I did not realize what was going on. Here was my code: php:<? while( !feof( $fh )){ $data[] = fgets( $fh ) ; foreach( $data as $n => $string ) if( preg_match( "/@/" , $string )){ $string = preg_replace( "/\r\n/" , "" , $string ) ; $string = preg_replace( "/@/" , "" , $string ) ; $string = "'".$string."'\r\n" ; fwrite( $fh1 , $string ) ; } elseif( preg_match( "/%/" , $string )){ $string = preg_replace( "/\r\n/" , "" , $string ) ; $string = preg_replace( "/%/" , "" , $string ) ; $string = "'".$string."'\r\n" ; fwrite( $fh1 , $string ) ; } else{ continue; } unset( $string ) ; }?> After many hours of smoking weed and trying different variations and lots of yelling, I realized that my fault was in using $string in the fwrite. There is a bug when you use foreach and do not redefine the array strings before writing them. The fix was simple: php:<? while( !feof( $fh )){ $data[] = fgets( $fh ) ; foreach( $data as $n => $string ) if( preg_match( "/@/" , $string )){ $string = preg_replace( "/\r\n/" , "" , $string ) ; $string = preg_replace( "/@/" , "" , $string ) ; $string = "'".$string."'\r\n" ; $name[$n] = $string ; } elseif( preg_match( "/%/" , $string )){ $string = preg_replace( "/\r\n/" , "" , $string ) ; $string = preg_replace( "/%/" , "" , $string ) ; $string = "'".$string."'\r\n" ; $seq[$n] = $string ; } else{ continue; } unset( $string ) ; } for( $i = 0 ; $i <= count( $data ) ; $i++ ){ $j = $i + 1 ; fwrite( $fh1 , $name[$i] ) ; fwrite( $fh1 , $seq[$j] ) ; }?> Apok fucked around with this message at 07:54 on Apr 23, 2010 |
# ? Apr 23, 2010 07:48 |
|
Apok posted:I hope this helps another newbie who is having the same problem! I now can sleep easy tonight knowing I have conquered a major programming dilemma! Forgive me if I've not followed what you're doing correctly, but why are you using foreach() to iterate over the whole array "data" every time you go through the loop? It looks like only the most recently added array element is of importance each time through. That would explain why you were seeing many duplicated lines of output with the first version of your code. The second version "fixes" that problem after a fashion, but appears to carry out many useless calculations reaffirming values stored in arrays "names" and "seq" that are not going to change.
|
# ? Apr 23, 2010 09:53 |
|
php:<?php // Connect to the database mysql_connect('localhost', 'root', ''); mysql_select_db('db'); $fetch = mysql_query("SELECT usual_bar, usual_kitchen, usual_waiting, usual_cleaning FROM `shifts`"); $display = mysql_fetch_assoc($fetch); ?> code:
I have this table on my project site; I have a read only version of this table which looks like; When that green link is clicked it takes you to the 'edit' form (top picture) to which you can the edit these values. What i'm trying to do is to get the values from the DB to populate the text boxes so the user can see what was in there previously before editing, and then a confirm button will save these values. Is the SQL statement to do this going to be too wild? How would everyone go about doing this? Is the command to save all the values going to be too long?
|
# ? Apr 23, 2010 18:11 |
|
I've got a script that takes a CSV file and generates records in a database. I use fgetcsv to handle the parsing, but it trips up on Windows-style line endings or whatever comes from Excel, and MAC line endings. Is there a silver bullet REGEX replace that will convert all wild line endings into UNIX-style?
|
# ? Apr 23, 2010 18:19 |
|
greasy digits posted:I've got a script that takes a CSV file and generates records in a database. I use fgetcsv to handle the parsing, but it trips up on Windows-style line endings or whatever comes from Excel, and MAC line endings. Is there a silver bullet REGEX replace that will convert all wild line endings into UNIX-style? Before you open the file: php:<? ini_set('auto_detect_line_endings',true); ?>
|
# ? Apr 23, 2010 19:32 |
|
Thirteenth Step posted:Is the SQL statement to do this going to be too wild? How would everyone go about doing this? It doesn't look that bad, but I think we'd need to see the table structure to know more. You know about the SQL help thread, right? http://forums.somethingawful.com/showthread.php?threadid=2672629 quote:Is the command to save all the values going to be too long? If you're worried about passing MySQL a string that's so long it crashes or something, don't. I can't say for sure, but I'd bet you'd hit your script memory limit before you made a string so big that it would crash the database (if that's even possible). Also, your dataset doesn't look very big.
|
# ? Apr 23, 2010 19:38 |
|
Hammerite posted:Forgive me if I've not followed what you're doing correctly, but why are you using foreach() to iterate over the whole array "data" every time you go through the loop? It looks like only the most recently added array element is of importance each time through. That would explain why you were seeing many duplicated lines of output with the first version of your code. The second version "fixes" that problem after a fashion, but appears to carry out many useless calculations reaffirming values stored in arrays "names" and "seq" that are not going to change. As I said, I have absolutely no training in any true coding at all (MSSQL doesn't count). I was forced to learn it. The code works for what I want. How could I change this? Is there a better way?
|
# ? Apr 23, 2010 19:39 |
|
php:<? while( !feof( $fh )){ $string = fgets( $fh ) ; $string = preg_replace( "/\r\n/" , "" , $string ) ; if( strpos($string,'@') !== false){ $string = str_replace( '@' , "" , $string ) ; }elseif( strpos($string,'%') !== false){ $string = str_replace( '%' , "" , $string ) ; } $string = "'$string'\r\n" ; fwrite( $fh1 , $string ) ; } ?> Munkeymon fucked around with this message at 14:56 on Apr 26, 2010 |
# ? Apr 23, 2010 19:50 |
|
Munkeymon posted:
...or you could probably just `dos2unix $filename` before you read the file.
|
# ? Apr 23, 2010 20:56 |
|
preg_replace("/\r?\n|\r/", "\n", ...)?
|
# ? Apr 23, 2010 21:36 |
|
rt4 posted:...or you could probably just `dos2unix $filename` before you read the file. And he could remove the @s and %s and add single quotes around each line with other command line tools, but he's doing it in PHP for whatever reason
|
# ? Apr 23, 2010 22:07 |
|
Munkeymon posted:And he could remove the @s and %s and add single quotes around each line with other command line tools, but he's doing it in PHP for whatever reason Sure, but those backticks are PHP
|
# ? Apr 23, 2010 22:17 |
|
greasy digits posted:I've got a script that takes a CSV file and generates records in a database. I use fgetcsv to handle the parsing, but it trips up on Windows-style line endings or whatever comes from Excel, and MAC line endings. Is there a silver bullet REGEX replace that will convert all wild line endings into UNIX-style? code:
|
# ? Apr 24, 2010 00:44 |
|
Anybody here have some sort of template system or maybe general alternative to Smarty?
|
# ? Apr 24, 2010 11:11 |
|
Thirteenth Step posted:Is the SQL statement to do this going to be too wild? How would everyone go about doing this? Is the command to save all the values going to be too long? Use the same SQL that drives your read-only form, and add the attribute value='' into the inputs of the form using the results array in the same way you do with the table: php:<? echo "<input value='".$results['bar']."'></input>"; //or inline html version <input value='<? echo $results['bar']; ?>'></input> ?>
|
# ? Apr 24, 2010 13:55 |
|
drcru posted:Anybody here have some sort of template system or maybe general alternative to Smarty? Are you looking for something "lighter" than smarty, or a better MVC framework?
|
# ? Apr 24, 2010 15:43 |
|
gwar3k1 posted:Use the same SQL that drives your read-only form, and add the attribute value='' into the inputs of the form using the results array in the same way you do with the table: So simply doing this will work? The thing I can't seem to wrap my head around is the concept of identifying all of the values in the DB and what I need to type in order to get them all to display in the text boxes.
|
# ? Apr 24, 2010 16:25 |
|
Thirteenth Step posted:So simply doing this will work? Its odd that you know mysql_fetch_assoc() but don't understand how to output data. Are you following a tutorial and its incomplete or are you not grasping concepts? https://www.php.net has fantastic documentation for its functions. Just put in mysql_fetch_assoc to the search and look at the examples which show you what to do and what to expect. You need to develop your fetch routine to include a while to accomodate the different shifts. $display is an array of the fields returned from your query for every row returned. To access the value of usual_bar, you would use $display['usual_bar']; php:<? $tablerows = ""; // for every row returned while($display = mysql_fetch_assoc($fetch) { // Append ouput with current row $tablerows .= "<tr>\n"; //... $tablerows .= " <td><input ... value='".$display['usual_bar']."'></td>\n"; $tablerows .= " <td><input ... value='".$display['usual_kitchen']."'></td>\n"; //... $tablerows .= "</tr>"; } ?>
|
# ? Apr 24, 2010 16:55 |
|
OK, so Ive passed a bunch of variables over from a form using various inputs using get and I can grab them all but the check box stuff. The check boxes all have the name searchUtils and appear in the url bar as searchUtils=Heating+(gas)&searchUtils=Gas&searchUtils=Internet&searchUtils=Cable how do I get all of those variables? $searchUtils = $_GET['searchUtils']; only grabs the last one.
|
# ? Apr 24, 2010 17:34 |
|
First hit on google for "php checkbox": http://www.html-form-guide.com/php-form/php-form-checkbox.html It's not the greatest example, but it answers your question. Google first, ask second!
|
# ? Apr 24, 2010 17:58 |
|
rugbert posted:OK, so Ive passed a bunch of variables over from a form using various inputs using get and I can grab them all but the check box stuff.
|
# ? Apr 24, 2010 20:15 |
|
Lumpy posted:Are you looking for something "lighter" than smarty, or a better MVC framework? Something "lighter."
|
# ? Apr 24, 2010 22:51 |
|
I've been doing a lot of research on PHP frameworks and am thinking about porting one of my current projects into Yii. Does anyone have any experience with this framework? Any recommendations before I get started?
|
# ? Apr 25, 2010 02:10 |
|
drcru posted:Something "lighter." But what are you looking for that is more than just PHP itself?
|
# ? Apr 25, 2010 03:28 |
|
MrMoo posted:But what are you looking for that is more than just PHP itself? True, how do you guys do it? I've never tried before.
|
# ? Apr 25, 2010 07:17 |
|
drcru posted:True, how do you guys do it? I've never tried before. What exactly do you need, whether it's more or less than what PHP provides, or just plain different? For example, in the template system I use for my framework, one of its primary benefits is to guarantee well-formed XML. Maybe you give a poo poo about that, maybe you don't. Or maybe you just want to limit what your designers can do in the view portion of an MVC architecture. Like you want to make sure that arbitary SQL queries aren't executed at render time. That's a worthwhile goal, but do you need to enforce it in a secondary/proprietary domain language, or is a mandated convention good enough? Lots of people will tell you that PHP is already a templating language, so you might as well use it directly instead of adding another layer to it. They're kinda right, but here's the rub: PHP, straight out the box, is an astoundingly lovely templating language. Anyone who works with MVC in PHP long enough will eventually want a more practical way to abstract the view, and the only people who think "plain PHP is good enough" are either inexperienced hacks or Rasmus Lerdorf himself. Smarty is a lovely alternative because it's just PHP with a different syntax. Useful for limiting what can be performed in a view, but so bad at it that most implementations let you embed PHP into the templates anyway. Long story short: there's no way to make this story short. Here be dragons.
|
# ? Apr 25, 2010 08:08 |
|
php:<? // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<th><b><center><u> <STYLE> <!-- tr { background-color: #ffffff} .normal { background-color: #CCCCCC } //--> </style> {$field->name} </center></u></b></th>"; } echo "</tr>\n"; // printing table rows while($row = mysql_fetch_row($result)) { echo "<tr>"; // $row array created. foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td><input type='text' size='5' value='$cell'></td>"; echo "</tr>\n"; } ?> That is what it's outputting. As you can 'probably' work out, the headers are fine but the cells below need to be shifted along to the right by 2, and then I want to echo the contents from 2 fields in there that are aren't editable under 'day' and 'shift'. So really under 'day' and 'shift' I want to have 'Monday' and 'Morning' for example as labels and not in text boxes, which are stored in the db.
|
# ? Apr 25, 2010 10:16 |
|
That's an SQL question. Do you have the fields "day" and "shift" in the same table as your values? If so, select them in your query: select day,shift,bar,kitchen,waiting,cleaning. It'll output the 6 fields, giving you six columns (assuming each field has data in the database). I can't help but get the feeling this is a school project. You seem to be falling into a lot of bad habbits and I would speak to a lecturer for guidance if this is the case.
|
# ? Apr 25, 2010 10:58 |
|
gwar3k1 posted:That's an SQL question. Do you have the fields "day" and "shift" in the same table as your values? If so, select them in your query: It's a project that im working on for a place I used to work, i find I learn better by jumping in at the deep end and learning as I go along. Yeah the values I want there are in the same table and they are selected, it was more about outputting them in a non-editable form, because the output inside of the loop defines text boxes, which I guess is a we design thing, sorry.
|
# ? Apr 25, 2010 11:11 |
|
Thirteenth Step posted:It's a project that im working on for a place I used to work, i find I learn better by jumping in at the deep end and learning as I go along. Yeah the values I want there are in the same table and they are selected, it was more about outputting them in a non-editable form, because the output inside of the loop defines text boxes, which I guess is a we design thing, sorry. Ah okay, sorry. If you look at my previous example, you can access each array item individually by using $row['fieldname']. Try the following: php:<? // loop all returned rows while($row = mysql_fetch_assoc($query) { // loop all items within the $row array for(i = 0; i<=count($row)-1; i++) { // conditional select based on index of $row array switch($i) { // first two indexes case 0: case 1: // output non-field cell break; // all other indexes case else: // output field cell break; } } }?>
|
# ? Apr 25, 2010 14:48 |
|
|
# ? Jun 6, 2024 03:20 |
|
Thirteenth Step posted:It's a project that im working on for a place I used to work, i find I learn better by jumping in at the deep end and learning as I go along. Yeah the values I want there are in the same table and they are selected, it was more about outputting them in a non-editable form, because the output inside of the loop defines text boxes, which I guess is a we design thing, sorry. Please don't take this the wrong way, but you need to do one of two things 13th: 1) Sit down with the most basic of basic "This is what a variable is. A function is .... " books for a month or two, and work yourself up to posting forms and storing things in a DB. 2) Just pay somebody else to do your project for you. You can't possibly be getting paid enough for the amount of time you are spending on this trivial stuff. You keep bouncing from thread to thread asking the same types of questions over and over, which leads me to believe you don't grasp some very basic concepts that you really should before you try to write anything. It's not that we don't enjoy helping you, but you can't seem to apply things people teach you form one question to the next. Again, I'm not trying to be a douche, and the whole reason for these threads is to help, but if I posted "How do I paint my house red?" and somebody answered "Go buy red paint and a brush, then apply the paint to the house with the brush" I would hope that I wouldn't need to ask "OK, my house is red, but now I want to paint my garage blue... how do I do that?" a day or two later.
|
# ? Apr 25, 2010 17:04 |