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. Cheers! That done the trick!
  12. 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;
  13. 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
  14. 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
  15. 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
×
×
  • 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.