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
Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Firequirks posted:

I had been using the Form Helper functions to make my form
code:
<?php echo form_submit ('submit', "Add Ingredient"); ?>
so I tried it the video way by just writing out the HTML and not including the name. But without the name, it causes another problem:
code:
if ($this->input->post('submit')) {
    $this->Ingredient_model->add();
}
That if statement will never be true, because the name is relied upon for the post function. :(

Edit: Perhaps I should do it entirely like the video, where he has written a no-view insert function?

Yeah, his code is far from best practice, as it's a hack job to show how fast things can be in a hypothetical best-case world.

You can cheat if you really want the "simplicity" by either manually making the submit button, leaving the name off and changing your if statement:

code:
if( $this->input->post('some_other_field_name') )
{
  // blah
}
or you can do it the way you are doing it, and unset 'submit' before you process:

code:
if( $this->input->post('submit') )
{
  unset( $_POST['submit'] );
  // blah
}
EDIT: you can also see what <?php echo form_submit (false, "Add Ingredient"); ?> does to use the helper and still have no name attribute.

Adbot
ADBOT LOVES YOU

Ziir
Nov 20, 2004

by Ozmaugh
Thanks for all the suggestions guys. I think I like this one the most:

thedaian posted:

Other possible options, beyond what's already been suggested: Create a phone/address book. A todo list (with bonus 'email me when something is nearly due option). A simple Facebook Application. Any number of simple turn based games (tic tac toe, connect 4, etc).

Lot of it does depend on what your interests are and how much programming knowledge you already have.

More specifically, the todo list. I've been hearing some things about HTML5 being able to cache databases offline so I did some light reading on the subject, and it seems these databases are stored with javascript (forgive me if my terminology is wrong). I'd like to build an iPhone webapp similar to a todo list with local database storing, but also able to sync with a MySQL database so that all isn't lost if the person clears their cache or whatever. Is this possible? I guess this might not belong in this thread but I figure I'll try.

Firequirks
Apr 15, 2007


Lumpy posted:

or you can do it the way you are doing it, and unset 'submit' before you process:

code:
if( $this->input->post('submit') )
{
  unset( $_POST['submit'] );
  // blah
}
EDIT: you can also see what <?php echo form_submit (false, "Add Ingredient"); ?> does to use the helper and still have no name attribute.

Thanks for your help! I think the unset option is the best one for me as it keeps everything the most tidy. :) I expect these forms to grow in the future, so it is a treat to keep the code for handling them short. (Also I tried your last suggestion, and no, it still doesn't work because it prints out name="" in the form. Oh well!)

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
I have a function I use for cleaning user input that's expected to be a string. One of the things it does (dependent on an argument) is to run htmlspecialchars() on the string. I want it to just convert >, <, " and & into entities, which is what it says on the php site it should do. But I find that when users enter some non-western characters, like eastern european accented letters or Chinese characters, they get turned into character entities too, which I wasn't expecting. I would add the optional argument specifying a character set, but I don't know what to try, since it claims that specifying UTF-8 will have the same effect as leaving it at the default latin1. Why is it behaving like this?

gwar3k1
Jan 10, 2005

Someday soon

Hammerite posted:

I have a function I use for cleaning user input that's expected to be a string. One of the things it does (dependent on an argument) is to run htmlspecialchars() on the string. I want it to just convert >, <, " and & into entities, which is what it says on the php site it should do. But I find that when users enter some non-western characters, like eastern european accented letters or Chinese characters, they get turned into character entities too, which I wasn't expecting. I would add the optional argument specifying a character set, but I don't know what to try, since it claims that specifying UTF-8 will have the same effect as leaving it at the default latin1. Why is it behaving like this?

If you only want <, >, and & to be converted, just use str_replace:
< = &lt;
> = &gt;
& = & amp;

You may prefer to use strip_tags if that's all you're trying to do?

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

gwar3k1 posted:

If you only want <, >, and & to be converted, just use str_replace:
< = &lt;
> = &gt;
& = & amp;

You may prefer to use strip_tags if that's all you're trying to do?

Of course you're right, I don't know why I didn't just think to do that before.

I'm a bit wary of using strip_tags, because I want to allow through certain tags but I don't want to allow them to have any attributes. To be more specific, I've been doing something like the following:
code:
$text = htmlspecialchars($text);
$text = str_ireplace('&lt;b&gt;','<b>',$text);
$text = str_ireplace('&lt;b&gt;','<i>',$text);
$text = str_ireplace('&lt;/b&gt;','</b>',$text);
$text = str_ireplace('&lt;/i&gt;','</i>',$text);
$text = str_ireplace('[b]','<b>',$text);
$text = str_ireplace('[i]','<i>',$text);
$text = str_ireplace('[/b]','</b>',$text);
$text = str_ireplace('[/i]','</i>',$text);
and I don't really want to use something with more functionality but which I've less of a handle on how to use safely; I don't want someone to be able to sneak through something like <b onmouseover="javascript">

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
The documentation does seem to suggest that htmlspecialchars() just changes those four characters, so maybe I am blaming the wrong thing. Is it possible that because the webpage the form comes from is marked as charset=iso-8859-1, the user's browser is converting foreign characters to character entities before they're sent to the site? That would explain the behaviour I see.

gwar3k1
Jan 10, 2005

Someday soon
I don't know about your new question, but could you use a regex for allowing certain tags but knocking off attributes?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

gwar3k1 posted:

I don't know about your new question, but could you use a regex for allowing certain tags but knocking off attributes?

I've already been scolded for trying to use a regex on html.

Maybe try textile? I haven't used it myself but it's been mentioned here a couple times.

For my own BBcode type of thing that uses SA style tags I use htmlspecialchars, then str_replace to replace "[b]" with the appropriate html, then I load that string into a DOMDocument and extract the <body>.

Rat Supremacy
Jul 15, 2007

The custom title is an image and/or line of text that appears below your name in the forums

Hammerite posted:

I have a function I use for cleaning user input that's expected to be a string. One of the things it does (dependent on an argument) is to run htmlspecialchars() on the string. I want it to just convert >, <, " and & into entities, which is what it says on the php site it should do. But I find that when users enter some non-western characters, like eastern european accented letters or Chinese characters, they get turned into character entities too, which I wasn't expecting. I would add the optional argument specifying a character set, but I don't know what to try, since it claims that specifying UTF-8 will have the same effect as leaving it at the default latin1. Why is it behaving like this?

Quick question but why wouldn't you want stuff converted to entities?

Also, perhaps:

http://daringfireball.net/projects/markdown/
http://michelf.com/projects/php-markdown/

Would be a better option? It sounds like you're setting yourself up for an XSS attack.

Rat Supremacy fucked around with this message at 11:43 on Jan 19, 2010

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

haywire posted:

Would be a better option? It sounds like you're setting yourself up for an XSS attack.

Am I? :( I'm working quite hard to try and make sure that users can't get through any bad HTML (indeed any HTML apart from <b> and <i> tags without attributes), but I'm concerned that you think they might get something through. Could you give me more of an idea what you think I might be leaving myself open to?

gwar3k1
Jan 10, 2005

Someday soon

Hammerite posted:

Am I? :( I'm working quite hard to try and make sure that users can't get through any bad HTML (indeed any HTML apart from <b> and <i> tags without attributes), but I'm concerned that you think they might get something through. Could you give me more of an idea what you think I might be leaving myself open to?

If you really are just after bold and italic, just use simple BB code like fletcher pointed out: [b ]/[i ]. Remove all tags in your sanitation routine, leaving in your custom tags then process them after the sanitation. If your users don't read the instructions on how to emphasize their text properly, then gently caress 'em.

edit:

A question of my own: how would I go about implementing a tag system? For categories, I have a table with a list of categories and a tiny int id; I put the id in a category field with my article. Should I take the same approach and comma separate the tag ids? How do I search based upon tag? Where tagid like '%id%';?

gwar3k1 fucked around with this message at 21:38 on Jan 19, 2010

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

gwar3k1 posted:

A question of my own: how would I go about implementing a tag system? For categories, I have a table with a list of categories and a tiny int id; I put the id in a category field with my article. Should I take the same approach and comma separate the tag ids? How do I search based upon tag? Where tagid like '%id%';?

No, never every store keys in a comma delimited list, its a disaster.

Use an intermediate table with 2 fields in it, one is the tag ID, the other is the article ID. Then select all the article IDs that match a specific tag, or vice versa. Its a lot easier update or remove tags using this approach as well.

gwar3k1
Jan 10, 2005

Someday soon

Begby posted:

No, never every store keys in a comma delimited list, its a disaster.

Use an intermediate table with 2 fields in it, one is the tag ID, the other is the article ID. Then select all the article IDs that match a specific tag, or vice versa. Its a lot easier update or remove tags using this approach as well.

Of course, you're right, and I should have thought of that immediately.

The1ManMoshPit
Apr 17, 2005

I have script that receives a request from a user, validates the request against an external server and then sends back a relatively large file. Right now we are using readfile to return the file in the response, but that is using a large amount of memory and causing the server to choke in load tests.

Is there a way to return a large file from PHP that is more efficient on memory? Ideally, I'd like to have the file resident in memory no more than once, but I'm not sure if that's possible.

Edit: I tried using readfile_chunked as described in the comments here http://cn2.php.net/manual/en/function.readfile.php#48683 and it actually made the memory usage much, much worse.

The1ManMoshPit fucked around with this message at 22:38 on Jan 20, 2010

Mr.NaviPacho
Aug 15, 2007
FTBRG!

The1ManMoshPit posted:

I have script that receives a request from a user, validates the request against an external server and then sends back a relatively large file. Right now we are using readfile to return the file in the response, but that is using a large amount of memory and causing the server to choke in load tests.

Is there a way to return a large file from PHP that is more efficient on memory? Ideally, I'd like to have the file resident in memory no more than once, but I'm not sure if that's possible.

Edit: I tried using readfile_chunked as described in the comments here http://cn2.php.net/manual/en/function.readfile.php#48683 and it actually made the memory usage much, much worse.

I would need to see more details to tell you exactly what to do but have you ever looked into using curl?

The1ManMoshPit
Apr 17, 2005

I probably should have specified that the file I'm returning is local to my server running the PHP script, and there are only a few (right now actually only one) unique files that are ever returned. So the requests go like:
  • User sends a request to my server with a token
  • My server sends the token off to an external validation server
  • If the external server validates the token it will return an ID. My server sends back the file corresponding to the id provided by the validation server. This file is stored on my server's file system.
PHP loads a copy of the file into memory once for every request coming in even though I am returning the exact same file for each of these requests. This quickly overwhelms the server if a large number of requests come in simultaneously. Ideally there would be some way for me to just have the file loaded into memory already and have each instance of the script just read and output straight out of that memory.

MrMoo
Sep 14, 2000

The1ManMoshPit posted:

Is there a way to return a large file from PHP that is more efficient on memory? Ideally, I'd like to have the file resident in memory no more than once, but I'm not sure if that's possible.

Edit: I tried using readfile_chunked as described in the comments here http://cn2.php.net/manual/en/function.readfile.php#48683 and it actually made the memory usage much, much worse.

Read chunking is the compatibility method of using fpassthru():

http://hk2.php.net/manual/en/function.fpassthru.php

Xenos
Jun 17, 2005

The1ManMoshPit posted:

PHP loads a copy of the file into memory once for every request coming in even though I am returning the exact same file for each of these requests. This quickly overwhelms the server if a large number of requests come in simultaneously. Ideally there would be some way for me to just have the file loaded into memory already and have each instance of the script just read and output straight out of that memory.

Are you running against Apache? If so, do you have the ability to install new Apache modules? If so, give mod_xsendfile a try. This module offloads the actual serving of the file to the web server. Instead of reading the file and and outputting its contents, all your script has to do is emit a few headers.

apekillape
Jan 23, 2009

by Peatpot
Quick, likely not very bright question:

I have a table full of records that are all Timestamped, and I'd like to echo all the records currently in the table that have today's date on them.

What's the php SELECT whatnot to grab that? I have the rest of it crudely written out, I just can't seem to figure out how to write the select statement.

Thanks.

gwar3k1
Jan 10, 2005

Someday soon

apekillape posted:

Quick, likely not very bright question:

I have a table full of records that are all Timestamped, and I'd like to echo all the records currently in the table that have today's date on them.

What's the php SELECT whatnot to grab that? I have the rest of it crudely written out, I just can't seem to figure out how to write the select statement.

Thanks.

Use a date function to get todays date in the format yyyy-mm-dd and do a SELECT on the table where the timestamp field is like your date variable.

apekillape
Jan 23, 2009

by Peatpot

gwar3k1 posted:

Use a date function to get todays date in the format yyyy-mm-dd and do a SELECT on the table where the timestamp field is like your date variable.


So something like

$today = date("Y-m-d");

$query = "SELECT message FROM gimme WHERE timestamp LIKE '$today%'";

Is that right? I think I'm screwing up the variable in the query, at the very least.

DoctorScurvy
Nov 11, 2005
More of a passing curiosity, really

apekillape posted:

So something like

$today = date("Y-m-d");

$query = "SELECT message FROM gimme WHERE timestamp LIKE '$today%'";

Is that right? I think I'm screwing up the variable in the query, at the very least.
That should work but do it like this to avoid confusion with the word 'timestamp' which is also a mysql data type.

$query = "SELECT message FROM gimme WHERE `timestamp` LIKE '".$today."%'";

I've always preferred concatenation to putting variables right into the string. The characters around 'timestamp' are the key next to the 1 on most keyboards, with ~

The1ManMoshPit
Apr 17, 2005

Xenos posted:

Are you running against Apache? If so, do you have the ability to install new Apache modules? If so, give mod_xsendfile a try. This module offloads the actual serving of the file to the web server. Instead of reading the file and and outputting its contents, all your script has to do is emit a few headers.

This looks perfect I'll give it a try. Thanks!

apekillape
Jan 23, 2009

by Peatpot
Ugh, ok, one more.

I'm trying to do some html scraping, which I'm kind of 50-50 on. If the code isn't too complicated, I can usually whip something out. But I've never really understood what the hell I'm doing, I just kind of Frankenstein something together.

So, for example, I want to grab this page:

http://www.songmeanings.net/artist/directory/a/

And insert all of the artist names and urls to their pages into a database.

What's the best way to do this? I've tried looking at simplexml/xpath stuff, dom parsing, all kinds of things. I mostly get caught up on trying to grab the artist name from between the tags, I just don't understand how that works.

Is there a recommended tutorial for this somewhere or anything? Sorry to be a bother, I've been up all night trying to make sense of how this works and it's just starting to weigh on me.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

apekillape posted:

I'm trying to do some html scraping...I just kind of Frankenstein something together.

That sounds pretty normal when you're scraping HTML. I'd use DOMDocument to grab that information.

Do a getElementById() on the "listing_artists" div and grab the first child (the <table>). Then do a getElementsByTagName() and get all the <tr> elements in that table. From there you can get down into the <a> that links to the artist page, using the textContent of that anchor for the artist name.

If you get hung up just look for tutorials on using DOMDocument and if you get hung up after that, ask your question here!

apekillape
Jan 23, 2009

by Peatpot

fletcher posted:

Do a getElementById() on the "listing_artists" div and grab the first child (the <table>). Then do a getElementsByTagName() and get all the <tr> elements in that table. From there you can get down into the <a> that links to the artist page, using the textContent of that anchor for the artist name.

If you get hung up just look for tutorials on using DOMDocument and if you get hung up after that, ask your question here!

Haha, I have almost no idea what that means, but I've at least got some decent keywords to google up. Thanks a lot man, I'm sure I'll be back shortly.

KuruMonkey
Jul 23, 2004

apekillape posted:

Haha, I have almost no idea what that means, but I've at least got some decent keywords to google up. Thanks a lot man, I'm sure I'll be back shortly.

Whatever tool you use (I usually brute force it with regex/strpos) you're really only (manually, visually) looking for a pattern in the html they use which you can latch onto and then repeatedly strip out.

I usually look for <tag class="SOMETHING" or something similar that will identify the beginning of each 'record'.

Scraping html is, in general, in every way a dirty job; but sometimes you have to do it.

apekillape
Jan 23, 2009

by Peatpot
Lemme throw up an example.

code:
<div id="listing_artists">
	<table>
		<tr>
			<th id="artists">Artists</th>
			<th id="lyrics">Lyrics</th>

		</tr>
		<tr class='row1'><td><a href="/artist/view/songs/6024/">A</a></td><td class='alignCenter'>76</td></tr>
<tr class='row0'><td><a href="/artist/view/songs/137438972714/">A Balladeer</a></td><td class='alignCenter'>16</td></tr>
<tr class='row1'><td><a href="/artist/view/songs/137438992366/">A Band Called Bert</a></td><td class='alignCenter'>1</td></tr>	</table>

</div>
I chopped off the billion other artist names (and added a carriage-return at <tr class='row'> to keep from breaking tables), but that's the gist. It alternates between row0 and row1 like that, which throws me way off to start with. But ya, that's what I'm dealing with.

I just want to grab the Artist Name and URL and throw them into a sql database, nothing really that fancy. I just... can't seem to wrap my head around how that's done. I can steal code here and there to loop through the links, but the main issue I'm having is figuring out which bit to modify so that it grabs the name cleanly and adds it with the proper associated link to drop into the DB.

If it's not asking way too much, could pretty much anyone throw up some sample code for at least the bit where that one part is done? I'm pretty good at catching on when the individual parts are explained, for some reason this one thing is just blowing my mind up though.

KuruMonkey
Jul 23, 2004

apekillape posted:

If it's not asking way too much, could pretty much anyone throw up some sample code for at least the bit where that one part is done? I'm pretty good at catching on when the individual parts are explained, for some reason this one thing is just blowing my mind up though.

This assumes the page is saved in the same directory as 'subject.htm' and cheats by using the fact that the ungodly huge single line is where all the actual target data is.

It makes a nice neat associative array, with keys f the artists and the urls and counts inside it.

Its quick, brute force, and ugly. But it (seems to) work on that page. Its basically just a case of trimming the data to the target section, splitting it into unparsed records with preg_split and then parsing each chunk with preg_match.

php:
<?php

    $parsed_records = array();
    
    $ifh fopen('subject.htm''r');
    if($ifh)
    {
        while(!feof($ifh))
        {
                $line rtrim(fgets($ifh));
                if(strlen($line) > 5000// its the huge line of all data!
                {
                    $records preg_split("/<tr class=.*>/U"$line);
                    foreach($records as $record)
                    {
                        $matches = array();
                        $result preg_match('/href=\"(.*)\">(.*)<.*alignCenter.*>([0-9]+)</U'$record$matches);
                        if($result 0)
                        {
                            $parsed_records[$matches[2]] = array('href'=>$matches[1], 'count'=>(int)$matches[3]);
                        }
                    }
                }
        }
        fclose($ifh);
    }

    // $parsed_records is an array("artist"=>array("href"=>url, "count"=>integer))
    foreach($parsed_records as $k=>$v)
    {
        echo $k.': '.print_r($vTRUE).'<br>';
    }

sample output:
code:
A: Array ( [href] => /artist/view/songs/6024/ [count] => 76 )
A Balladeer: Array ( [href] => /artist/view/songs/137438972714/ [count] => 16 )
A Band Called Bert: Array ( [href] => /artist/view/songs/137438992366/ [count] => 1 )
A Band Called Pain: Array ( [href] => /artist/view/songs/137438979732/ [count] => 1 )
A Band of Bees: Array ( [href] => /artist/view/songs/137438963565/ [count] => 10 )
A Beautiful Demise: Array ( [href] => /artist/view/songs/17463/ [count] => 4 )
A Beautiful Lotus: Array ( [href] => /artist/view/songs/137438988918/ [count] => 0 )
A Beautiful Silence: Array ( [href] => /artist/view/songs/137438980500/ [count] => 12 )
A Billion Ernies: Array ( [href] => /artist/view/songs/137438963846/ [count] => 9 )
A Bird A Sparrow: Array ( [href] => /artist/view/songs/137438984028/ [count] => 8 )
A Black Rose Burial: Array ( [href] => /artist/view/songs/137438974243/ [count] => 6 )
A Blinding Silence: Array ( [href] => /artist/view/songs/137438965981/ [count] => 11 ) 
You will have to deal with the fact that all the URLs are relative, and the grabbing of the source into a file is left to you.

KuruMonkey fucked around with this message at 01:15 on Jan 22, 2010

apekillape
Jan 23, 2009

by Peatpot

KuruMonkey posted:

This assumes the page is saved in the same directory as 'subject.htm' and cheats by using the fact that the ungodly huge single line is where all the actual target data is.

It makes a nice neat associative array, with keys f the artists and the urls and counts inside it.

Its quick, brute force, and ugly. But it (seems to) work on that page. Its basically just a case of trimming the data to the target section, splitting it into unparsed records with preg_split and then parsing each chunk with preg_match.

You will have to deal with the fact that all the URLs are relative, and the grabbing of the source into a file is left to you.

:psypop:

Owe you huge, man. I'll be back with victory or at least a lot of questionably hacked together data.

Update: OH MY GOD I THINK I CAN MAKE THIS WORK.

I had to refer back to a bunch of random tutorials to look up how to manipulate arrays properly, and I thought I might still be sunk because frankly I'm kind of a moron. Then I referred back to your post briefly so I could put the code back after I screwed it up, and out of the corner of my eye I saw where it said associative arrays.

Suddenly it all made sense. Thank you so very much.

apekillape fucked around with this message at 01:49 on Jan 22, 2010

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

KuruMonkey posted:

Its quick, brute force, and ugly. But it (seems to) work on that page. Its basically just a case of trimming the data to the target section, splitting it into unparsed records with preg_split and then parsing each chunk with preg_match.

Ummm really? The DOMDocument solution is so much cleaner...

Does that really look like code this guy can modify/learn from to you?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Here you go apekillape:

php:
<?php
        error_reporting(1);
        $url "http://www.songmeanings.net/artist/directory/a/";

        //download the page first
        $ch curl_init($url);
        curl_setopt($chCURLOPT_RETURNTRANSFER1);
        curl_setopt($chCURLOPT_HEADER0);
        $result curl_exec($ch);
        curl_close($ch);

        //now process it
        $doc = new DOMDocument();
        $doc->loadHTML($result);

        $mainDiv $doc->getElementById("listing_artists");
        $anchors $mainDiv->getElementsByTagName("a");

        foreach ($anchors as $anchor) {
                $artist $anchor->textContent;
                $artistUrl $anchor->getAttribute("href");
                echo $artist." - ".$artistUrl."\n";
        }
?>

Now go figure out how to get it into a database. Use PDO.

DoctorScurvy
Nov 11, 2005
More of a passing curiosity, really
Terrific example, fletcher, I've been wondering how to do that.

apekillape
Jan 23, 2009

by Peatpot
You're both beautiful people and have completely saved my bacon.

Looping and getting it into the DB and all was never a problem, I just learned everything way out of order. I think this might fix a lot of my mental scraping issues though, shazam this thread is the best.

DarkLotus
Sep 30, 2001

Lithium Hosting
Personal, Reseller & VPS Hosting
30-day no risk Free Trial &
90-days Money Back Guarantee!

apekillape posted:

You're both beautiful people and have completely saved my bacon.

Looping and getting it into the DB and all was never a problem, I just learned everything way out of order. I think this might fix a lot of my mental scraping issues though, shazam this thread is the best.

This will get A through Z
php:
<?php
foreach(range('a','z') as $i) {
    $url "http://www.songmeanings.net/artist/directory/$i/";
    $artistHTML artistcURL($url);
    $artists_to_insert artistList($artistHTML);
    
    $insert_sql join(", "$artists_to_insert);
    // Simple Database insert
    mysql_query("INSERT INTO table (name, url) VALUES $insert_sql");
}


function artistList($html) {
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $mainDiv $doc->getElementById("listing_artists");
    $anchors $mainDiv->getElementsByTagName("a");
    
    foreach ($anchors as $anchor) {
        $artist mysql_real_escape_string($anchor->textContent);
        $artistUrl mysql_real_escape_string("http://www.songmeanings.net".$anchor->getAttribute("href"));
        $artist_to_insert[] = "('$artist', '$artistUrl')";
    }
    return $artist_to_insert;
}

function artistcURL($url) {
    $curl curl_init();
    curl_setopt($curlCURLOPT_RETURNTRANSFER,1);
    curl_setopt($curlCURLOPT_URL$url);
    $result curl_exec($curl);
    curl_close($curl);
    return $result;
}
?>

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

DarkLotus posted:

This will get A through Z

<snip>


Why are you telling somebody new to PHP to use mysql_real_escape_string :ughh: prepared statements! PDO!

fletcher fucked around with this message at 05:46 on Jan 22, 2010

DarkLotus
Sep 30, 2001

Lithium Hosting
Personal, Reseller & VPS Hosting
30-day no risk Free Trial &
90-days Money Back Guarantee!

fletcher posted:

Why are you telling somebody new to PHP to use mysql_real_escape_string :ughh: prepared statements! PDO!

He already had the database side down.
Quote:
"Looping and getting it into the DB and all was never a problem"


You might as well ask why I would tell somebody to use mysql_real_escape_string or mysql_insert without first opening the database connection :q:

Italian Stalin
Jul 4, 2003

You-a gonna get-a purged!
I'm building a small program for work which will be used when we check in/out laptops and other electronics equipment for student use. As of now, I've coded a form with HTML and PHP that will check out an item, but I'm having difficulty creating a form that will check in/renew items.

What I had in mind was a query that would check to see which items were checked out by a user and then, based on the number of items checked out, a dynamic list would populate with checkboxes next to them indicating either a renew option or a checkin option. So, if there were two items checked out, a list of two items would show up with checkboxes next to each item. Unfortunately, I'm not sure how to code a dynamically generated list like that. I can only code a list of a static size.

How would I code a dynamic list of this sort?

Adbot
ADBOT LOVES YOU

MrMoo
Sep 14, 2000

armed2010 posted:

How would I code a dynamic list of this sort?

Which bit is the problem? I hope you have saved the items checked out with some unique id somewhere. Then to check-in you pull that list of items, iterate over them generating the HTML as necessary for your check boxes, etc.

php:
<?
foreach ($items as &$item)
{
?>
  <input type="checkbox" name="<?=$item->name;?>">
<?
}
?>

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