mouseywings
-
Posts
20 -
Joined
-
Last visited
Posts posted by mouseywings
-
-
You could build a separate array of keys and use that for access. However you'd have to build it. Which leads to... I may have another way but would be easier to see the get_posts() function! However, when you say cache, are you storing all these in a session variable? Else, you're retrieving it all again anyway...!?
I'm using Laravel for the get_posts() so I'm not sure if you could follow along. I'm storing all of these in a cache file (.txt), not a session.
-
Ok so this is how my collection looks when pulling from the database:
array (size=43)
0 =>
array (size=
'id' => int 1
'topic_id' => int 1
'user_id' => int 3
'body' => string '<p>Right after this I will change my permissions and see if I can only reply and not create!</p>' (length=96)
'hidden' => int 0
'likes' => int 1
'created_at' => string '2015-04-08 13:00:34' (length=19)
'updated_at' => string '2015-04-19 16:34:06' (length=19)
1 =>
array (size=
'id' => int 2
'topic_id' => int 1
'user_id' => int 3
'body' => string '<p>Seeing if I can reply!</p>' (length=29)
'hidden' => int 0
'likes' => int 0
'created_at' => string '2015-04-08 14:08:40' (length=19)
'updated_at' => string '2015-04-19 16:34:08' (length=19)
2 =>
array (size=
'id' => int 3
'topic_id' => int 1
'user_id' => int 3
'body' => string '<p></p>
<p>Testing reply as an artist! <br /></p>' (length=53)
'hidden' => int 0
'likes' => int 1
'created_at' => string '2015-04-09 14:06:46' (length=19)
'updated_at' => string '2015-04-19 16:34:13' (length=19)
3 =>
array (size=
'id' => int 4
'topic_id' => int 1
'user_id' => int 3
'body' => string '<p>Testing a new comment!</p>' (length=29)
'hidden' => int 0
'likes' => int 0
'created_at' => string '2015-04-15 14:53:05' (length=19)
'updated_at' => string '2015-04-15 14:53:05' (length=19)
.................However when it gets to be over 100 items long.. I'm trying to make it so if I make an edit to ID 5, I can JUST make an edit to that one and re-cache it without having to flush all those items out of the cache and then regrab them.. because that will get pretty hectic I think especially if they are edited like every 2 minutes or whatever. Is there a way to make the array INDEX the value of the post ID? Or would I have to loop through them after grabbing them to do so? I just want to be able to make it like:
$data = $this->getPosts( $id ); // This will grab that array that you see above
$data[$id]->body = $newBody;
// And then code to recache the data (overwrite the cache key with the new data)Thanks for any help!
-
You cannot use XMLHttpRequest (Ajax) to contact an external website without their cooperation. Either they need to allow it by setting the appropriate headers or they need to provide an alternative such as JSONP.
What you can do is proxy the request via your server by using PHP and cURL to contact the other site then having your Ajax code communicate with your PHP script instead of the other site directly.
I'm trying to do my research, but I can't see if cURL allows you to access that domain without them changing their headers and accepting our domain to talk to theirs. I didn't know if you knew if you could do it without them having to issue something.. or if there was a nice tutorial on how to go about doing this.
-
Will CURL allow me to hit their webpage without them having to set out any headers?
-
Don't you have to add that to the domain you are trying to reach? It's apart of their domain. I would never be able to get them to look into the code or whatever because it's a huge company.
-
The thing is is that they aren't going to add headers on their side just for our website.. I looked into that, but both parties need to be in communication.
-
I want to know how to hit a webpage that is not on your domain. I am doing an AJAX request right now, but receiving the following error in my console:
XMLHttpRequest cannot load https://bpb.opendns.com/a/www.playboy.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://filter.localhost' is therefore not allowed access.
The site I'm trying to hit does this weird cookie thing. I already spoke with their web administrator and he said the first time they access that site, it sets the cookie so they have to refresh in order to see if they can bypass a webpage they are trying to access..
I'm not sure if there is anyway to go about doing this since I can't go the AJAX route. But I don't have the capability of refreshing the user once they go on that page since it's not under my server or whatever. The only thing I could think of was AJAX which is not an option now.
<?php include('scripts/config.php'); //Declare Variables $filter = new Filter(); $byPasscode = $filter->getBypassCode(); $openDns = "https://bpb.opendns.com/a/" . $_GET['url']; echo 'Redirecting...'; ?> <html> <head></head> <body> <form method="post" name="bypassForm" id="bypassForm" action="<?php echo $openDns ?>" style="display:none"> <input type="text" size="15" name="textBypassCode" id="textBypassCode" autocomplete="off" value="<?php echo $byPasscode; ?>" /> <input type="password" size="15" name="passwordBypassCode" id="passwordBypassCode" style="display:none;" value="<?php echo $byPasscode; ?>" /> <input type="hidden" name="code" id="bypassCode" value="<?php echo $byPasscode; ?>" /> <input type="submit" value="Continue" /> </form> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> //setTimeout(function(){ var textBypassCode = $('#textBypassCode').val(); var passwordBypassCode = $('#passwordBypassCode').val(); var bypassCode = $('#bypassCode').val(); $.ajax({ type: 'POST', url: $('#bypassForm').attr('action'), data: { textBypassCode: textBypassCode, passwordBypassCode: passwordBypassCode, bypassCode: bypassCode } }) .done(function(data) { //$('#bypassForm').submit(); }); //}, 3000); </script> </body> </html>
Up above you can see my code. Thanks for any other options that you may think of.
-
The "similar methods" thing is inheritance and polymorphism at work: code doesn't necessarily need to know the exact type of item, just that it's an item and can be use()d.
Factories can be used a lot, even to the point of factories of factories, and for the most part all you're doing is adding a bit of (worthwhile) overhead. If you find yourself needing if or switch blocks to construct the different types of classes, a factory is probably a good idea.
- Items probably could use a factory. Somehow you can identify an item, right? Like the string "food" or "weapon" or "toy". Perhaps with some other information useful to the item itself. A factory can be used there: if you can standardize on a constructor, like
public function __construct(array $config) {
then you can use a factory.public static function factory($item, array $config) { // if your item classes are all named uniformly, like "FoodItem", and you have an item identifier "food" $class = ucfirst($item) . "Item"; if (class_exists($class)) { return new $class($config); } else { trigger_error("No such item '{$item}'", E_USER_WARNING); return null; } }
- Tutorials don't actually sound like they need a factory. I'd imagine that a tutorial is "started" in very specific places in code, and so adding more tutorials is not just a matter of writing a new class.- Random events probably have a bit of information attached to each, like the weight/probability of the event, but it's quite possible that you can use a factory here like you can for items.
Thanks for your reply! Cleared it up a bit for me =)
This is what I had so far in my head for the Item Factory:
<?php interface IItemRepo { public function use( $id ); } class ItemFactory { public function make( $id ) { $item = $this->model->getById( $id ); if($item->type == 1) { return new FoodItemRepo( $item ); } } } abstract class ItemRepo implements IItemRepo { protected $type; protected $model; public function __construct(Item $item) { $this->model = $item; } public function setType( IItemRepo $type ) { $this->type = $type; } abstract function use( array $attributes ); public function remove( $id ) { $this->model->whereId( $id )->delete(); } public function removeAll( $id ) { $this->model->whereItemId( $id )->delete(); } } class FoodItemRepo extends ItemRepo { public function __construct(Item $item) { $this->model = $item; $this->setPet( new Pet ); } public function setPet( IPetRepo $pet ) { $this->pet = $pet; } public function use( array $attributes ) { // Raise health $this->pet->raiseHunger( $attributes->pet ); // Remove item from inventory $this->remove( $attributes->item ); } } // www.sitename.com/inventory/item/remove/29 // www.sitename.com/inventory/item/use/29 class Item { public function setItem( $item ) { $this->item = ItemFactory::make( $item ); } public function getUse( $id ) { $this->data['item'] = $this->item->get(); $this->render('content', 'inventory.item.index', $this->data); } public function postUse( $id ) { $this->item->use( Input::all() ); } }
Am I going about the Factory::make() the right way? Passing the Item Model to it so it grabs it's type by there?
-
So I was stumbling around with Repositories and Interfaces and got a decent grasp on it. However, I started running into abstraction and started to implement a little bit of the Factory design.
How often is this typically used? It seems like I should be using Factory with 90% of my Repositories since there are different types of Items, Tutorials, Random Events, etc.
Items
- Food
- Weapon
- Toy
Tutorials
- Newbie
- Farming
Random Events
- Coin Giving
- Item Giving
- Item Draining
Things like this... so all of them have similar methods involved. So they each have a base method, for example.. an Item child class would always have the use() method and a factory would determine which kind of object to initiate from your controller just from the item model being passed through. So therefore you can call use() on the repository instance it returns.
Then I was moving onto my tutorials and random events and it just seemed very similar.
So in most cases, will factories be a huge plus? Just wanted some general opinions from the PHP developers =)
Thanks!
-
I got it working like this:
public function findKey( $doc, $key ) { $doc = preg_match("/<body[^>]*>(.*?)<\/body>/is", $doc, $matches); $list = explode('<BR>', $matches[1]); foreach($list as $l) { $pair = explode('=', $l); if(trim($pair[0]) == $key) { return $pair[1]; } } return null; }
Thanks
-
It's not letting me edit my post for some odd reason. But after a little digging around, I got this to display so far:
string ' REC INFO[p!]=p EXP DATE[p43]=03-20-15 PCODE1[p44]=1 PCODE2[p45]=u'... $doc = file_get_contents('http://IPADDRESSTOPIN/PATRONAPI/21333008706699/dump'); $doc = trim(str_replace("<BR>", "", $doc)); $doc = preg_match("/<body[^>]*>(.*?)<\/body>/is", $doc, $matches); die(var_dump($matches[1]));
I tried exploding that file, but it still did the same thing with mild changes in the results. So far that's what I have. The code is on the bottom of the result.
Not sure how to get what I want after that =/
-
If what you are saying (and this is a guess since you aren't very clear) is that the 4 lines above represent key=value pairs, simply explode each line on the = sign and then add the 2 array elements to an array as in :
list($k,$v) = explode("=",$line); $arr[$k] = $v;
Of course this will have to go into some kind of loop to collect all the data (4 lines). Now you can simply interrogate the array for the value you want:
$val = $arr['PCODE[p45]'];
The problem is is that it's taking the break lines into consideration... This is what it returns:
die(var_dump($k)) returns this:
string '<HTML><BODY> REC INFO[p!]' (length=25)
And die(var_dump($v)) returns this:
string 'p<BR> EXP DATE[p43]' (length=19)
I have no idea what it's even doing.
I just have 4 lines. And sometimes it could be more. I just want to get the value of a key. Sometimes I'll get the p45 one, sometimes I'll need the p44. Or whatever other key I'll need in the list.
When I put the returned api string in a die() method, it returns this:
<body> REC INFO[p!]=p<br> EXP DATE[p43]=03-20-15<br> PCODE1[p44]=1<br> PCODE2[p45]=u<br> </body>
That's the HTML at least..
-
So this is how the data is returned for the API I'm using:
REC INFO[p!]=p EXP DATE[p43]=03-20-15 PCODE1[p44]=1 PCODE2[p45]=u
So I'm trying to filter on either of the keys like 'PCODE2[p45]' or 'PCODE1[p44]'.. I'm trying to get the value after the equal sign, but still remain on the same line. So if I were searching by the p45 key, I would get 'u'. If I was searching by the p44 key I would get 1..
Is there a simple regex or something that would allow me to achieve these results?
Thank you for any help!
-
I don't want the user to be able to control the shading strengths though..? I was using GD as well, but ImageMagick seems to offer so much more control so I switched over to that. Your screenshot is just a screenshot of phpfreaks..
-
Here is the code I have right now:
<?php header('Content-type: image/png'); $grey = new ImagickPixel('#ebebeb'); $orange = new ImagickPixel('#0f92d6'); $image = new Imagick('assets/lop.png'); $layer = new Imagick('assets/lop-base.png'); $layer->thumbnailImage(200, 0); $image->thumbnailImage(200, 0); $layer->paintOpaqueImage($grey, $orange, 100); // Place layer on image $image->compositeImage($layer, Imagick::COMPOSITE_SOFTLIGHT, 0, 0); // Let's merge all layers (it is not mandatory). $image->flattenImages(); echo $image; ?>
It's putting the layers on top of eachother very nice. The thing is is that this involves some heavy shading at times depending on the bunny breeds.
Here is what the base image ($image) looks like:
And here is what the overlay ($layer) looks like:
This is what the outcome looks like versus the intended result:
I've tried almost all of their composite constants.. I can't figure out how to do this or the best way of going about it. Because the colors are going to be dynamic throughout the site so I can't just save a .png of every color combination.. and there will be more colors layered on top of eachother like markings/patterns/etc.
Thanks for any help!
-
I'm wondering if the GD library is capable of coloring ONLY the non-transparent areas in an image. I had this image white, black, and every other color under the moon, but could only get GD to color the entire canvas (width and height) of the image with an opacity over it. I'm merging many layers on top of each other so I can't afford to color the entire square and lower the opacity and do some weird multiply stuff.
That's the image above. Then I have some lineart, markings, eye color, etc. So I had all my possible colors white areas that I layer on top of eachother. I want to color the entire non-transparent areas with a color (dynamic).. so I can't just color it in Photoshop and then upload it or else I would literally have millions of possibilities for every single color and shade..
Thanks for any help. I was trying to go with ImageMagick if GD isn't capable, but I can't even get that downloaded in my Wamp. I'm using 64 bit Wamp so I have no idea if that does something or whatever. I can get it installed on my computer and run it via command line, but I can't get it to work and render images in my Wamp files... can't ever find a .dll that works.
-
@Barand, you are a genius Thank you so much!!!!
-
I have the following code below:
<?php
$xml = '<Divisions xmlns="urn:description7a.services.chrome.com">
<responseStatus responseCode="Successful" description="Successful"/>
<division id="1">Acura</division>
<division id="44">Aston Martin</division>
<division id="4">Audi</division>
<division id="45">Bentley</division>
<division id="5">BMW</division>
<division id="6">Buick</division>
<division id="7">Cadillac</division>
<division id="8">Chevrolet</division>
<division id="9">Chrysler</division>
<division id="11">Dodge</division>
<division id="46">Ferrari</division>
<division id="59">FIAT</division>
<division id="13">Ford</division>
<division id="170">Freightliner</division>
<division id="15">GMC</division>
<division id="16">Honda</division>
<division id="17">Hyundai</division>
<division id="18">Infiniti</division>
<division id="19">Isuzu</division>
<division id="20">Jaguar</division>
<division id="21">Jeep</division>
<division id="22">Kia</division>
<division id="47">Lamborghini</division>
<division id="23">Land Rover</division>
<division id="24">Lexus</division>
<division id="25">Lincoln</division>
<division id="48">Lotus</division>
<division id="53">Maserati</division>
<division id="26">Mazda</division>
<division id="61">McLaren</division>
<division id="27">Mercedes-Benz</division>
<division id="2">MINI</division>
<division id="29">Mitsubishi</division>
<division id="30">Nissan</division>
<division id="34">Porsche</division>
<division id="57">Ram</division>
<division id="49">Rolls-Royce</division>
<division id="52">Scion</division>
<division id="42">Smart</division>
<division id="37">Subaru</division>
<division id="56">Tesla</division>
<division id="39">Toyota</division>
<division id="40">Volkswagen</division>
<division id="41">Volvo</division>
</Divisions>
';
$parse = simplexml_load_string($xml);
print_r($parse);The problem is is that it's not keeping the attribute values in the xml like
<division id="1">Acura</division>
<division id="44">Aston Martin</division>The PHP is erasing it to be like:
array (
0 => 'Acura',
1 => 'Aston Martin'
);I'm passing this to JSON so I need it to stay in the format of an array, but need the indexes to be the value of the division ID attribute. This is what it's exactly outputting right now:
SimpleXMLElement Object (
[responseStatus] => SimpleXMLElement Object (
[@attributes] => Array (
[responseCode] => Successful
[description] => Successful
)
)
[division] => Array (
[0] => Acura
[1] => Aston Martin
[2] => Audi
[3] => Bentley
[4] => BMW
[5] => Buick
[6] => Cadillac
[7] => Chevrolet
[8] => Chrysler
[9] => Dodge
[10] => Ferrari
[11] => FIAT
[12] => Ford
[13] => Freightliner
[14] => GMC
[15] => Honda
[16] => Hyundai
[17] => Infiniti
[18] => Isuzu
[19] => Jaguar
[20] => Jeep
[21] => Kia
[22] => Lamborghini
[23] => Land Rover
[24] => Lexus
[25] => Lincoln
[26] => Lotus
[27] => Maserati
[28] => Mazda
[29] => McLaren
[30] => Mercedes-Benz
[31] => MINI
[32] => Mitsubishi
[33] => Nissan
[34] => Porsche
[35] => Ram
[36] => Rolls-Royce
[37] => Scion
[38] => Smart
[39] => Subaru
[40] => Tesla
[41] => Toyota
[42] => Volkswagen
[43] => Volvo
)
)Thank you for any help!
-
So I'm generating an XML response for Google Maps and this is how my method looks:
/* * Generate XML for google maps * @param results Database results array */ public function generate_xml(array $results) { // Start XML file, create parent node $dom = new DOMDocument("1.0"); $node = $dom->createElement("markers"); $parnode = $dom->appendChild($node); header("Content-type: text/xml"); /* * Iterate through the rows, adding XML nodes for each * Have to do it the ugly way so results aren't pulled twice * (xml and php can't be passed within same array because headers are changed) */ foreach ($results as $row){ $node = $dom->createElement("marker"); $newnode = $parnode->appendChild($node); $newnode->setAttribute('id', $row['id']); $newnode->setAttribute('logo', $row['logo']); $newnode->setAttribute("name", $row['name']); $newnode->setAttribute("address", $row['address']); $newnode->setAttribute('city', $row['city']); $newnode->setAttribute('state', $row['state']); $newnode->setAttribute('zip', $row['zip']); $newnode->setAttribute('phone', $row['phone']); $newnode->setAttribute('email', $row['email']); $newnode->setAttribute('web_link', $row['web_link']); $newnode->setAttribute("lat", $row['lat']); $newnode->setAttribute("lng", $row['lng']); $newnode->setAttribute("distance", $row['distance']); } return $dom->saveXML(); }
However, I'm getting this error:
error on line 2 at column 1: Extra content at the end of the documentBelow is a rendering of the page up to the first error.I believe I'm nesting all my nodes correctly... I'm not really understanding where I'm not. Thanks for any help!
Need more insight on caching when pagination is involved
in PHP Coding Help
Posted · Edited by mouseywings
Well right now in my method, I'm grabbing ALL results (from a cache) and then paginating those results. If I cache during the pull, wouldn't I have to do like Page 1 cache, Page 2 cache and end up storing like 50 cache results per topic if it were to hit 50 pages? That would be an insane amount of caches per topic when I could just do one. That's why I was trying to keep it that way.
I just dislike how I have to flush the entire cache and recache the entire topic posts if just one post was edited or liked.