Jump to content

MoFish

Members
  • Posts

    227
  • Joined

  • Last visited

Everything posted by MoFish

  1. Thank you for your prompt response Jacques1. Yes, you are right that worked perfectly. I think i got a little confused with the array_push which took me off track. Thanks again. MoFish
  2. Hi, I'm trying to structure an array into the following format: Array ( [Barrow-in-Furness] => Array ( [11] => Isle of Walney [12] => Dalton-in-Furness [13] => Askam-in-Furness [14] => Millom [15] => Ulverston ) ) I can get my data into this format using the following code which arranges the array correctly and works, however i get lots of errors due to the array_push requiring two parameters. I have tried to resolve this by adding more parameters, but it keeps changing the array structure i require. Warning: array_push() expects at least 2 parameters, 1 given in /home5/whammond/public_html/sharpmedia/gts/controller.inc.php on line 114 Strict Standards: Only variables should be passed by reference in /home5/whammond/public_html/sharpmedia/gts/controller.inc.php on line 116 Warning: array_push() expects at least 2 parameters, 1 given in /home5/whammond/public_html/sharpmedia/gts/controller.inc.php on line 116 Strict Standards: Only variables should be passed by reference in /home5/whammond/public_html/sharpmedia/gts/controller.inc.php on line 116 array_push($array[$location]); array_push($array[$location][$i]); array_push($array[$location][$i]=$connection->sheets[0]["cells"][$i][2]); Would anyone be able to advise on how i can manipulate the three lines of code above to maintain the array structure i require above, and also fix the warnings . error messages received. Thanks, MoFish
  3. Thank you for your reply. That explains why that was occouring. I have changed my array to your first example - however the foreach seems like it needs tweeking How would i amend this to display the results: London - North London - South London - West Newcastle - North
  4. Hi, I have an array: $areas = array( "London" => "North", "London" => "South", "London" => "West", "Newcastle" => "North" ); I'm trying to loop around each one using the following: foreach ($areas as $region => $city) { echo $city . "<br/>"; The results i'm getting is the bottom two. It seems like it wants unique ones. West North I expected it to loop around them all and print north, south, west, north. Can someone advise me how to write out each one? Why would it be skipping two values? Thanks, MoFish
  5. Hi Barand, I didn't really want to save back to the file ideally... I'll see if I can find an alternative. Thanks, MoFish
  6. Hi Barand, No problem, i'll try to figure it out. I appriciate all the help you have gave me thus far. Regards, MoFish
  7. Morning Barand, You are correct in that moving the get file contents outside of the loop resolved the populating of data. I moved it inside the loop as wanted to get the location from the DB from the 'template' field, like the following. foreach($result as $row){ $str = file_get_contents("templates/".$row['template'].".html", true); I'm assuming from your response above, this technique would not work? Thanks.
  8. HI Barand, When used your example code, both replacements worked well. Once changing to the DB it only seems to be doing one for some reason. <div class="col-xs-3 text-center"> <h4><mofish id="title" type="text" label="Title" /></h4> <p>Once upon a time...</p></div> Below is the two functions you helped with. I've amended things slightly but not to drastically. public function area($name){ $query = "SELECT areas_content.*, areas.* FROM areas_content INNER JOIN areas ON areas.id = areas_content.area_id WHERE area_id = '41' "; $this->database->sql($query); $result = $this->database->getResult(); echo "<pre>"; print_r($result); echo "</pre>"; foreach($result as $row){ // set replacement to be the string from db $str = file_get_contents("templates/staff.html", true); $replace = $row['content']; $srch = $row['location']; $str = $this->replace($srch, $replace, $str); } echo '<pre>',htmlentities($str),'</pre>'; // check output } public function replace($srch, $replace, $str) { $p2=0; $found = 0; while(($p1 = strpos($str, '<mofish', $p2)) !== false) { $p2 = strpos($str, '/>', $p1); $x = (substr($str,$p1,$p2-$p1+2)); // template contains template1 or template2 if (strpos($x, "id=\"$srch\"")) { $found = 1; break; } } if ($found) { return str_replace($x, $replace, $str); } else return $str; } And the array output Array ( [0] => Array ( [id] => 41 [content] => Hello world [area_id] => 41 [location] => title [name] => Staff [template] => staff => Home ) [1] => Array ( [id] => 41 [content] => Once upon a time... [area_id] => 41 [location] => description [name] => Staff [template] => staff => Home ) ) And inside staff.html <div class="col-xs-3 text-center"> <h4><mofish id="title" type="text" label="Title" /></h4> <p><mofish id="description" type="text" label="Description" /></p> </div>
  9. Hi Barand, I managed to get your code working, however my $result array from the database returns slightly differently than your example. Mine has keys, which i think is breaking things slightly. Array ( [0] => Array ( [blah] => 41 ) [1] => Array ( [blah] => 42 ) )
  10. Hi Barand, If you don't mind, I have one more issue I have come across which relates to the code you helped me with. This if probably something simple I've done, however would appreciate it if you could lend a helping hand, as have been looking at it for a while now. I have amended the code so that results are coming from a database which seems to be working good to a certain degree... However the results I am getting back are not quite as I expected. Please see the example below: <?php public function run (){ $str = '<div class="col-xs-3 text-center"> <h4><mofish id="template1" type="text" label="Title" /></h4> <p><mofish id="template2" type="text" label="Description" /></p> </div>'; // for each row in database foreach($result as $row){ // set replacement to be the string from db $replace = $row['string']; $p2=0; $found = 0; while(($p1 = strpos($str, '<mofish', $p2)) !== false) { $p2 = strpos($str, '/>', $p1); $x = (substr($str,$p1,$p2-$p1+2)); // template contains template1 or template2 if (strpos($x, 'id="'.$row['template'].'"')) { $found = 1; break; } } if ($found) { $newstr = str_replace($x, $replace, $str); } // i think i need to concatenate these somehow echo $newstr; // return $newstr; } } ?> I am getting results returning to the browser, however it seems to be taking it in turns for each instance. I had hoped it would have done both replacements in each instance. <div class="col-xs-3 text-center"> <h4>PHP Freaks</h4> <p><mofish id="template2" type="text" label="Description" /></p> </div> <div class="col-xs-3 text-center"> <h4><mofish id="template1" type="text" label="Title" /></h4> <p>This is a question</p> </div> Expected <div class="col-xs-3 text-center"> <h4>PHP Freaks</h4> <p>This is a question</p> </div> <div class="col-xs-3 text-center"> <h4>Another One</h4> <p>This is a question</p> </div> It is probably something to do with the way I'm echoing them, and possibly need to be concatenating the results together then returning it as one string? Would that be correct? Hope this makes sense, MoFish
  11. Hi Barand, Found a slight problem. If you put in an ID that doesnt exist, it still does the replace. Is there anyway to just return blank if its not found? $str = '<div class="col-xs-3 text-center"> <h4><mofish id="number33" type="text" label="Title" /></h4> <p><mofish id="number25" type="text" label="Description" /></p> </div>'; $replace = 'whatever'; $p2=0; while(($p1 = strpos($str, '<mofish', $p2)) !== false) { $p2 = strpos($str, '/>', $p1); $x = (substr($str,$p1,$p2-$p1+2)); if (strpos($x, 'id="number2"')) { break; } } $newstr = str_replace($x, $replace, $str); echo $newstr;
  12. Thanks so much Barand, that appears to be doing everything for the time being. No doubt i'll get stuck again sometime soon! Thanks, MoFish
  13. Hi, Thanks for your replies and appologies in being a bit slow coming back to you. akphidelt2007s answer done exactly as required, as i only required the 'whatever' value to be returned. Thank you! I do however need to amend the pattern slightly, so it only gets mofish tags with specific ids. For example say i only wanted to run the preg_replace where id = 'number2' $str = '<div class="text"> <mofish id="number1" type="number1" title="true" /> </div> <div class="text"> <mofish id="number2" type="number2" title="true" /> </div>'; Regards, MoFish
  14. Hi, I'm having a bit of bother doing a bit of string manipulation. I have the following HTML in a variable called $data <div class="text"> <mofish id="testing" type="testing" title="true" /> </div> I'm trying to replace the <mofish id="text" type="text" title="true" /> part with a value, for example the word "Hello". However, I dont want to have to hard code into the string replace the attribute values, as these could potentially change. Ideally I would like it to do a string replace on the start of the <mofish tag until the closing tag /> without taking into consideration any of the attributes. I have tried using str_replace but think i'm maybe off tangent? Would this be a regex job? Any guidance much appriciated. Thanks, MoFish
  15. Hello, I do appologise gizmola, I overlooked your reply on my phone. You seem to have hit the nail on the head, and this query is now calculating as expected. Thank you for both replying and again, appologise for the confusion. Regards, MoFish
  16. Thanks for your response Barand. So adapting your query above to not include the update statement, what should be query look like? Thanks, MoFish
  17. Hello, Thank you for your replies. I'm not looking up update the product table with a value of total, ideally this would just be an alias on the select query. You are both correct in that table_a and table_b are identical, but this is for demonstrations sake to paint the picture. The tables are different and have differing fields. I'm assuming following on from your above queries, if I do something like the below with the appropriate joins, I would then be able to display the 'total' on my page without having the field in the table? SELECT product_id, SUM(price) as total Please correct me if I am mistaken or have misunderstood Thanks again guys, MoFish
  18. Hi, I wondered if anyone could help me. Please see the attached screenshot as an example of my DB. I'm looking to write a SQL query which adds a 'total' value into the product table. This will do a SUM on product_a.price and product_b.price WHERE product.product_id equals a specific id. I have tried to get this working, but am struggling. Any help much appriciated. Thanks, MoFish.
  19. Thank you Ch0cu3r, worked a treat!
  20. Hi, I'm looking for a little guidance on the best way to loop around some form input fields. I have a form with a few input boxes. I have used the [] on the end of the names in an attempt to get these into an array. View <input type="text" class="form-control" id="url[]" name="url[]" placeholder="" value="" /> <input type="text" class="form-control" id="price[]" name="price[]" placeholder="199" value="" /> I'm now trying to get each of the values out and save them to the database. I assume I need a foreach loop to do this, but am not sure on the best way to do this. I have the following in my controller so far. Controller $post_array = array( 'url' => $_POST['url[]'], 'price' => $_POST['price[]'], 'order_id' => '1' ); $this->order_model->add($post_array); Any help/guidance would be much appriciated! Thanks, MoFish
  21. Hi, I have the following code which works well. $this->data['results'] = $this->domain_status_model->get(); foreach($this->data['results'] as $result){ $result->domainstatus_items = $this->domain_model->get_by_status($result->domainstatus_id); I would like the ability to target my array a little more specifically. I therfore am trying to replace the domainstatus_item portion using a returned value from another query. When I try this, it does not appear to be working, probably due to a simple mistake. I have tried using . + and & operators in order for it to work, however am not having much success. $this->data['results'] = $this->domain_status_model->get(); foreach($this->data['results'] as $result){ $result->domainstatus_$result->domainstatus_title = $this->domain_model->get_by_status($result->domainstatus_id); In summary, the value returned from $result->domainstatus_title is for example 'pending' and I am trying to get the following: $result->domainstatus_pending = $this->domain_model->get_by_status($result->domainstatus_id); Any help would be much appriciated. Kind regards, MoFish
  22. Hey Ch0cu3r. Thanks for the solution. I'll give this a go later on tonight and let you know how I get on. Looks to be the perfect solution. Cheers, Guy
  23. Hi, I currently have some html stored in a variable as below: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> Hello world </body> </html> This requires a "<div class='container'>" to be inserted directly after the opening body tag. My original thinking was to use str_replace on the <body> to be <body><div class="container"> however note that occasionally my body will require an ID or Class. I therefore need a way to make it appear after the body tag, even if it has an ID or Class on it . I assume this may be reg-ex, but am not sure if there is a simple way around this? Would anyone be able to clarify and point me in the right direction. Much appreciated. MoFish
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.