I am using this code for explode and show GET variables. But I would like remove current query in the link:
My explode code:
$k = $_GET['sef'];
$s_explode = explode("-",$k);
foreach($s_explode as $q) {
if($q==$s_explode[0]) {
echo '<a class="active" href="/category/'.$q.'">'.$s_explode[0].' <span class="dismiss">×</span></a>';
} else {
echo ''.$q.' <span class="dismiss">×</span>';
}
}
If I using GET
website.com/?sef=game-book-video
Print is:
<a class="active" href="/category/game">game</a>
book
video
I would like if I using GET
website.com/?sef=game-book-video
<a class="active" href="/category/book-video">game</a>
book
video
I hope I can explain good sorry for my bad English.
Your code would be like this:
$k = $_GET['sef'];
$s_explode = explode("-", $k);
//game-book-video
foreach($s_explode as $i => $q) {
$parts = $s_explode;
if(($key = array_search($q, $parts)) !== false) {
unset($parts[$key]);
}
$class = ($i == 0 ? "class='active'" : '');
echo '<a ' . $class . ' href="/category/'.implode('-', $parts).'">'.$q.' <span class="dismiss">×</span></a>';
}
Related
I cant seem to figure out how to achieve my goal.
I want to find and replace a specific class link based off of a generated RSS feed (need the option to replace later no matter what link is there)
Example HTML:
<a class="epclean1" href="#">
WHAT IT SHOULD LOOK LIKE:
<a class="epclean1" href="google.com">
May need to incorporate get element using DOM as the Full php has a created document. If that is the case I would need to know how to find by class and add the href url that way.
FULL PHP:
<?php
$rss = new DOMDocument();
$feed = array();
$urlArray = array(array('url' => 'https://feeds.megaphone.fm')
);
foreach ($urlArray as $url) {
$rss->load($url['url']);
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue
);
array_push($feed, $item);
}
}
usort( $feed, function ( $a, $b ) {
return strcmp($a['title'], $b['title']);
});
$limit = sizeof($feed);
$previous = null;
$count_firstletters = 0;
for ($x = 0; $x < $limit; $x++) {
$firstLetter = substr($feed[$x]['title'], 0, 1); // Getting the first letter from the Title you're going to print
if($previous !== $firstLetter) { // If the first letter is different from the previous one then output the letter and start the UL
if($count_firstletters != 0) {
echo '</ul>'; // Closing the previously open UL only if it's not the first time
echo '</div>';
}
echo '<button class="glanvillecleancollapsible">'.$firstLetter.'</button>';
echo '<div class="glanvillecleancontent">';
echo '<ul style="list-style-type: none">';
$previous = $firstLetter;
$count_firstletters ++;
}
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
echo '<li>';
echo '<a class="epclean'.$i++.'" href="#" target="_blank">'.$title.'</a>';
echo '</li>';
}
echo '</ul>'; // Close the last UL
echo '</div>';
?>
</div>
</div>
The above fullphp shows on site like so (this is shortened as there is 200+):
<div class="modal-glanvillecleancontent">
<span class="glanvillecleanclose">×</span>
<p id="glanvillecleaninstruct">Select the first letter of the episode that you wish to get clean version for:</p>
<br>
<button class="glanvillecleancollapsible">8</button>
<div class="glanvillecleancontent">
<ul style="list-style-type: none">
<li><a class="epclean1" href="#" target="_blank">80's Video Vixen Tawny Kitaen 044</a></li>
</ul>
</div>
<button class="glanvillecleancollapsible">A</button>
<div class="glanvillecleancontent">
<ul style="list-style-type: none">
<li><a class="epclean2" href="#" target="_blank">Abby Stern</a></li>
<li><a class="epclean3" href="#" target="_blank">Actor Nick Hounslow 104</a></li>
<li><a class="epclean4" href="#" target="_blank">Adam Carolla</a></li>
<li><a class="epclean5" href="#" target="_blank">Adrienne Janic</a></li>
</ul>
</div>
You're not very clear about how your question relates to the code shown, but I don't see any attempt to replace the attribute within the DOM code. You'd want to look at XPath to find the desired elements:
function change_clean($content) {
$dom = new DomDocument;
$dom->loadXML($content);
$xpath = new DomXpath($dom);
$nodes = $xpath->query("//a[#class='epclean1']");
foreach ($nodes as $node) {
if ($node->getAttribute("href") === "#") {
$node->setAttribute("href", "https://google.com/");
}
}
return $dom->saveXML();
}
$xml = '<?xml version="1.0"?><foo><bar><a class="epclean1" href="#">test1</a></bar><bar><a class="epclean1" href="https://example.com">test2</a></bar></foo>';
echo change_clean($xml);
Output:
<foo><bar><a class="epclean1" href="https://google.com/">test1</a></bar><bar><a class="epclean1" href="https://example.com">test2</a></bar></foo>
Hmm. I think your pattern and replacement might be your problem.
What you have
$pattern = 'class="epclean1 href="(.*?)"';
$replacement = 'class="epclean1 href="google.com"';
Fix
$pattern = '/class="epclean1" href=".*"/';
$replacement = 'class="epclean1" href="google.com"';
I want to display the student names from db and two buttons for each students.
In my code i am using ajax function.
controller
function get_sib_filter()
{
$list_id= $this->input->post('id2');
if(($list_id)==1)
{
$filtered_students = $this->home_model->filter_by_sibling();
$new_string = "";
foreach($filtered_students->result() as $detail)
{
$new_string.=$detail->applicant_first_name;
$new_string=$new_string.'Selected For Interview';
$new_string=$new_string.'Rejected</br>';
}
echo $new_string;
}
}
But i got the last name only(last name and two buttons)
I want list all name and all name having two buttons
Plzz give suggetions..
Like mentioned in the comments above, here are the issues found:
you never defined $new_string
you are overwriting $new_string
you are not concatenating the base_url the right way
Also, since you are using base_url you should be loading the URL helper in your controller ($this->load->helper('url')) or autoloading it in autoload.php
Try this:
$students = $filtered_students->result();
$string = "";
foreach ($students as $student) {
$string .= $student->applicant_first_name . " ";;
$string .= "<a href='" . base_url() . "home/change_filter_status_green/$student->applicant_id' class='btn green button_style' title='Filter'>Selected For Interview</a> | ";
$string .= "<a href='" . base_url() . "home/change_filter_status_red/$student->applicant_id'' class='btn red but_style' title='Rejected'>Rejected</a><br/>";
}
echo $string;
You are overwriting the string here. Please make some changes as shown below.
$new_string = ""; // Define $new_string..
foreach($filtered_students->result() as $detail){
$new_string.=$detail->applicant_first_name;
$new_string.='Selected For Interview';
$new_string.='Rejected<br />';
}
And also make sure what are you getting from this variable: $filtered_students.
You can check it by print_r($filtered_students);
Try the following code:
function get_sib_filter()
{
$list_id= $this->input->post('id2');
if($list_id == 1)
{
$filtered_students = $this->home_model->filter_by_sibling();
$new_string = "";
$count = 0;
foreach($filtered_students->result() as $detail)
{
if($count == 0){
$new_string = "<a href='".base_url('home/change_filter_status_green/').$detail->applicant_id."' class='btn green button_style' title='Filter'>Selected For Interview</a><a href='".base_url('home/change_filter_status_red').$detail->applicant_id."' class='btn red but_style title='Rejected'>Rejected</a>";
$count++;
}else{
$new_string .= $new_string."<a href='".base_url('home/change_filter_status_green/').$detail->applicant_id."' class='btn green button_style' title='Filter'>Selected For Interview</a><a href='".base_url('home/change_filter_status_red').$detail->applicant_id."' class='btn red but_style title='Rejected'>Rejected</a>";
}
}
echo $new_string;
}
}
change your model like this:
function filter_by_sibling()
{
$this->db->select('applicant_id,applicant_first_name');
$this->db->from('student_application');
$this->db->order_by('sib_count','desc');
$result = $this->db->get()->result_array();
return $result;
}
Modify your controller function like this:
function get_sib_filter()
{
$list_id= $this->input->post('id2');
if(($list_id)==1)
{
$filtered_students = $this->home_model->filter_by_sibling();
$new_string = "";
foreach($filtered_students as $detail)
{
$new_string .=$detail['applicant_first_name'];
$new_string .='Selected For Interview';
$new_string .='Rejected<br />';
}
echo $new_string;
}
}
This is the code which loads images with different Prefix and the prefix is printed under every image.
I need Prefix as a Title of every set of images as Category of Images
CODE :
$images = glob($dirname . "*.jpg");
foreach ($images as $image) {
if (strpos($image, '#') !== false) {
} else {
?>
<li><?php $mn_img = str_replace("/thumbs", "", $image); ?>
<div class="th [radius]">
<a href="<?php echo $mn_img ?>"><img align="middle"
src="<?php echo $image; ?>"> </a>
</div>
<p style="text-align: center">
<?php
$str = $mn_img;
$s = end(explode("/", $str));
$e = explode(".", $s);
$n = explode('-', $e[0]);
$nm = $n[0];
if ($nm === 'non') {
echo 'general';
} else {
$title = str_replace("_", " ", $nm);
echo $title;
}
?>
</p>
</li>
<?php }
}
I expect the possibility :)
MY Prefix Format is
Manager_fileoriginalname_random.jpg
Manager_fileoriginalname_random.jpg
Manager_fileoriginalname_random.jpg
Manager_fileoriginalname_random.jpg
Prefix is "Manager"
Matketing_fileoriginalname_random.jpg
Matketing_fileoriginalname_random.jpg
Matketing_fileoriginalname_random.jpg
Matketing_fileoriginalname_random.jpg
Prefix is "Marketing"
is the preg_match function is what you want :
$returnValue = preg_match( '/(.*)_.*_.*$/', 'Manager_fileoriginalname_random.jpg', $matches );
in $matches[1] you have the prefix, you just need to remplace 'Manager_fileoriginalname_random.jpg' by your var $mn_img
If i can make and advise, can use parse_url() to extract url components : http://php.net/manual/fr/function.parse-url.php
This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 9 years ago.
My original string is
one,two,three,four,five,
I need separate each word as a link
one, two, three, ...
My code is
$plat = $row['reg'];
foreach ($plat as $key => $pv) {
$pl[] = implode(',', $pv);
}
for ($p = 1; $p = sizeof($pl); $i++) {
echo '' . $pl[i] . '';
}
This would suffice..
<?php
$str='one,two,three,four,five';
$arr=explode(',',$str);
foreach($arr as $val)
{
echo "<a href=''>$val</a>, ";
}
OUTPUT :
<a href=''>one</a>, <a href=''>two</a>, <a href=''>three</a>, <a href=''>four</a>, <a href=''>five</a>
<?php
$str='one two three four five';
$arr=explode(' ',$str);
foreach($arr as $val)
{
echo "<a href=''>$val</a>, ";
}
this Code to separate blank spacesepration
$string = 'one,two,three,four,five';
$tag_open = '<a href="#" rel="tag">';
$tag_close = '</a>';
echo $tag_open. implode($tag_close.', '.$tag_open, explode(',', $string)). $tag_close;
Try this code:
$plat=$row['reg'];
foreach ($plat as $key=> $pv) {
$pl[] = explode(',', $pv);
for($p=1;$p=sizeof($pl); $i++) {
echo ''.trim($pl[i]).'';
}
}
I´ve got the a php that returns a JSON string:
$recipes = json_encode($arr);
That is my php-code how I output the recipe-title:
<?php
include('php/getAllRecipes.php');
$jsonstring = $recipes;
$recip = json_decode($recipes, true);
$i = 1;
var data = include('php/getAllRecipes.php')Data.Recipes;
foreach ($recip['Data']['Recipes'] as $key => $recipe) {
echo "$i.)   ";
echo $recipe['TITLE'];
$i = $i + 1;
echo "<br>";
}
?>
Now, I need to add a href to each title. The href should contain a link to recipe_search.php and I have to give it the id of each recipe.
How can I add this href?
<?php
include('php/getAllRecipes.php');
$jsonstring = $recipes;
$recip = json_decode($recipes, true);
?>
<ol>
<?php
foreach ($recip['Data']['Recipes'] as $key => $recipe) {
echo '<li>
<a href="/recipe_search.php?id=' . $recipe['ID'] . '">
' . $recipe['TITLE'] . '
</a>
</li>';
}
?>
</ol>
Use an ordered list (<ol>) instead of trying to create one yourself using a counter.
var data = include('php/getAllRecipes.php')Data.Recipes; is not valid PHP.
I assume that the id of the recipe is in $recipe['ID'].
Here you are...
foreach ($recip['Data']['Recipes'] as $key => $recipe)
{
// I guess $key is ID of your recipe...
echo sprintf('%d.) %s<br />', $i++, 'recipe_search.php?id=' . $key, $recipe['TITLE']);
}
Thats worked for me, just to test the above:
<?php
$i = 1;
foreach (array_fill(0, 40, 'recipe') as $key => $recipe)
{
// I guess $key is ID of your recipe...
echo sprintf('%d.) %s<br />', $i++, 'recipe_search.php?id=' . $key, $recipe);
}
?>