PHP: show an image in php script - php

I want to implement this <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /> into the following function.
function list_my_categories($exclude = '') {
if (!empty($exclude)) {
$exclude = 'exclude=' . $exclude;
}
$categories = get_categories($exclude);
$html = '';
foreach ($categories as $cat) {
if($cat->category_parent == 0) {
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
$html .= '<p>' . $cat->cat_name . '</p>';
$html .= '<p>' . $cat->category_description . '</p>';
$childcategories = get_categories('child_of='.$cat->cat_ID.'');
if(!empty($childcategories)){
foreach ($childcategories as $ccat) {
$html .= '<p>' . $ccat->cat_name . '</p>';
$html .= '<p>' . $ccat->category_description . '</p>';
}
}
}
}
return $html;
}
add_shortcode('show-cats', 'list_my_categories');
The function shows a list of categories in WordPress, and I have another plugin which allows you to set an image for each category, so i am trying to show it...

if($cat->category_parent == 0)
{
$html .= '<img src="' . z_taxonomy_image_url($cat->term_id) . '" />';
$html .= '<p>' . $cat->cat_name . '</p>';
$html .= '<p>' . $cat->category_description . '</p>';
$childcategories = get_categories('child_of='.$cat->cat_ID.'');
if(!empty($childcategories))
{
foreach ($childcategories as $ccat)
{
/*$html .= '<img src="' . z_taxonomy_image_url($ccat->term_id) . '" />';*/
$html .= '<p>' . $ccat->cat_name . '</p>';
$html .= '<p>' . $ccat->category_description . '</p>';
}
}
}

Related

PHP Syntax error: Unexpected for after string "</div>"

I been trying to figure out if I can add a for loop in this situation:
$g = '<td id="'.$this->currentDate.'" class=" td-top-text '.($cellNumber%7==1?' start ':($cellNumber%7==0?' end ':' ')).
($cellContent==null?'mask':'').'">'
//if statement
.($this->currentDate === $tutor_date ?
'<div class="inside">'//open inside
.'<div class="inside-content">' //open inside-content
. '<div class="inside-date">'.$cellContent.'</div>'//open and close inside-date
for($i =0; $i < count($appt_date['date']); $i++) {
'<div class="inside-event '.$bkg_color.' ck-button btn-xs" title="Group Session">'//open inside-event
. '<label class="label-for-text">'//open label
. '<input type="radio" data-toggle="modal" data-target="#myModal" name="appt_selected" value="'.$tutor_shedule_id.'" >'//open input
. '<span>'.$tutor_info.'</span>' //open and close span
.'</input>'// close input
. '</label>'// close label
. '</div>';//close inside-event
}
'</div>'//close inside-content
. '</div>'//close inside
. '</td>' //close td
//else
: '<div class="inside">'.$cellContent.'</div>'
. '</td>' );
return $g;
I tried so many things such as added a ; at the end of </div> and before the for loop, I also added a . before the for loop and after.
EDIT:
Instead of down-vote this question, please provide some feedback and let me know what is wrong. For me this code is "fine" until you tell me a better way to do it. So, please I really appreciated your feedback.
Thanks.
I recommend you to do the if-else statement rather than this approach as this process is very difficult to debug. So, go with the following code:
$g = '<td id="'.$this->currentDate.'" class=" td-top-text '.($cellNumber%7==1?' start ':($cellNumber%7==0?' end ':' ')).
($cellContent==null?'mask':'').'">';
if ($this->currentDate === $tutor_date){
$g.='<div class="inside">'//open inside
.'<div class="inside-content">' //open inside-content
. '<div class="inside-date">'.$cellContent.'</div>';//open and close inside-date
for($i =0; $i < count($appt_date['date']); $i++) {
$g.='<div class="inside-event '.$bkg_color.' ck-button btn-xs" title="Group Session">'//open inside-event
. '<label class="label-for-text">'//open label
. '<input type="radio" data-toggle="modal" data-target="#myModal" name="appt_selected" value="'.$tutor_shedule_id.'" >'//open input
. '<span>'.$tutor_info.'</span>' //open and close span
.'</input>'// close input
. '</label>'// close label
. '</div>';//close inside-event
}
$g.= '</div>'//close inside-content
. '</div>'//close inside
. '</td>' ;//close td
}else{
$g.='<div class="inside">'.$cellContent.'</div>'
. '</td>'; }
return $g;
Well that was painful, I've tried to fix it up a little and at least make it more readable. Have a try of this... also I should point out I'm pretty sure this code is vulnerable to XSS attacks.
// Work out the Class names.
$classNameOne = ($cellNumber % 7) == 1 ? ' start '
: ($cellNumber % 7) == 0 ? ' end'
: ' ';
$classNameTwo = ($cellContent == null) ? 'mask'
: '';
// Build up the html.
$html = '<td id="' . $this->currentDate . '" class="td-top-text ' . $classNameOne . $classNameTwo . '">';
if ($this->currentDate === $tutor_date) {
$html .= '<div class="inside">';
$html .= '<div class="inside-content">';
$html .= '<div class="inside-date">' . $cellContent . '</div>';
$dateInstances = count($appt_date['date']);
for ($i = 0; $i < $dateInstances; $i++) {
$html .= '<div class="inside-event ' . $bkg_color . ' ck-button btn-xs" title="Group Session">';
$html .= '<label class="label-for-text">';
$html .= '<input type="radio" data-toggle="modal" data-target="#myModal~ name="appt_selected" value="'. $tutor_shedule_id .'">';
$html .= '<span>' . $tutor_info . '</span>';
$html .= '</input>';
$html .= '</label>';
$html .= '</div>';
}
$html .= '</div>';
$html .= '</div>';
$html .= '</td>';
} else {
$html .= '<div class="inside">' . $cellContent . '</div>';
$html .= '</td>';
}
return $html;

PHP combined with HTML code

I am trying to put some php code in a code that is combined with HTML.
I have the following code:
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
echo '<div class="groepitem"><input type="radio" [IN THIS PLACE] name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
On the [in this place] spot I want to put the following code:
if($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
echo 'checked';
}
I have tried it multiple times but I just can't get it to work.
You can concatenate a ternary operator like this:
$result = mysqli_query($db, "SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
echo '<div class="groepitem"><input type="radio" ' . ($_SESSION['gebruikers_klasid'] == $record['klas_id'] ? "checked" : "") . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
You can do this many different ways, but if you want cleaner html code:
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while ($record = mysqli_fetch_array($result)) {
if ($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
$checked = ' checked';
} else {
$checked = '';
}
echo '<div class="groepitem"><input type="radio"' . $checked . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
OR...
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while ($record = mysqli_fetch_array($result)) {
$checked = ($_SESSION['gebruikers_klasid'] == $record['klas_id']) ? ' checked' : '';
echo '<div class="groepitem"><input type="radio"' . $checked . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
Another way is to exit your echo then run the conditional statement then resume your echo... eg
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
echo '<div class="groepitem"><input type="radio" ';
if($_SESSION['gebruikers_klasid'] == $record['klas_id']) { echo 'checked'; }
echo ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
this should work
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
if($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
$var = 'checked';
}
echo '<div class="groepitem"><input type="radio" '.$var.' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
EDIT: now session check it's inside the loop

PHP query - on 1st, 5th, 9th record add a class

Im wondering if its possible with PHP to add a class to X record returned. I know I can do this with JS only I'd like it to have the class added as the records are returned.
I have the following loop in my PHP, From what I've found in google I need to add a counter to do this only I've been unsuccessful so far...
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo '<div class="entry span3"><span class="name">' . $row['First_Name'] . ' ' . $row['Surname'] . "</span>";
echo '<img src="' . $row["picture_1"] . '" alt="' . $row['First_Name'] . ' ' . $row['Surname'] . ', text ' . $row['Date'] . ' ">';
echo '<span class="from">seen in ' . ucwords($row["Location_County__Seen"]) . '</span>View Profile</div>';
}
In front of your while, add $c = 1
Before the end of your while loop, add $c++;
Then, modify your first line:
echo '<div class="entry span3"><span class="name">'
To
echo '<div class="entry span3';
if (($c % 4) == 1) echo ' newclassname ';
echo '"><span class="name">'
For the final result:
$c = 1;
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo '<div class="entry';
if (($c % 4) == 1) echo ' newclassname ';
echo ' span3"><span class="name">' . $row['First_Name'] . ' ' . $row['Surname'] . "</span>";
echo '<img src="' . $row["picture_1"] . '" alt="' . $row['First_Name'] . ' ' . $row['Surname'] . ', text ' . $row['Date'] . ' ">';
echo '<span class="from">seen in ' . ucwords($row["Location_County__Seen"]) . '</span>View Profile</div>';
$c++;
}

How to sort from for each in PHP

From an array I start for each entry a function with a argument from the array (its friends from facebook). The function returns either 0 or a number bigger than 0. So at the moment, it justs displays it all mixed up, but I want it to show first all entries which get the result bigger than 0 from the function and then all, which are 0. I've no idea, how to do this.
This is my current code.
foreach ($friends as $key=>$value) {
//removed unimportant things
$friendresult = friendscore($id = $fvalue[id]);
if ($friendresult == "0") {
echo '<li>';
echo '<a class="box" href="javascript:invite('. $fvalue[id] . ')">';
echo '<img src="https://graph.facebook.com/' . $fvalue[id] . '/picture" title="' . $fvalue[name] . '" />';
echo '<label>' . $fvalue[name] . '</label>';
echo '<b>Invite</b>';
echo '</a>';
echo '</li>';
}
if ($friendresult !=="0"){
echo '<li>';
echo '<a class="box">';
echo '<img src="https://graph.facebook.com/' . $fvalue[id] . '/picture" title="' . $fvalue[name] . '" />';
echo '<label>' . $fvalue[name] . '</label>';
echo '<div class="totaltext">Score:'.$friendresult. '</div>';
echo '</a>';
echo '</li>';
}}
Edit: Found solution. Sometimes its too simple.
if ($friendresult == "0") {
$friend.= '<li>';
$friend.= '<a class="box" href="javascript:invite('. $fvalue[id] . ')">';
$friend.= '<img src="https://graph.facebook.com/' . $fvalue[id] . '/picture" title="' . $fvalue[name] . '" />';
$friend.= '<label>' . $fvalue[name] . '</label>';
$friend.= '<b>Invite</b>';
$friend.= '</a>';
$friend.= '</li>';
}
if ($friendresult !="0"){
$nofriend.= '<li>';
$nofriend.= '<a class="box">';
$nofriend.= '<img src="https://graph.facebook.com/' . $fvalue[id] . '/picture" title="' . $fvalue[name] . '" />';
$nofriend.= '<label>' . $fvalue[name] . '</label>';
$nofriend.= '<div class="totaltext">Score:'.$friendresult. '</div>';
$nofriend.= '</a>';
$nofriend.= '</li>';
}
echo $friend.$nofriend;

Could this code be cleaner?? It looks a right state?

I have this code to pull data from the database and insert the data into one of 4 columns,
I have spent a whole day searching and just cant seem to find out how to do it..
Ideally, I want to select all from database and then where the fetch array has a column id of 1 - echo that then the sama for the next column etc..
<?php
echo '<div class="column grid_3 clearfix" id="column0" >';
echo ' ';
$user_sites_0=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='0' ORDER BY sort_no");
if(!$user_sites_0) {
echo 'No sites added, please <a class="addsite" href="#">add one now</a>';
}
else {
while($user_site_0=mysqli_fetch_array($user_sites_0))
{
$id = stripslashes($user_site_0['id']);
$site_name = stripslashes($user_site_0['site_name']);
$site_address = stripslashes($user_site_0['site_address']);
$site_desc = stripslashes($user_site_0['site_desc']);
$site_category = stripslashes($user_site_0['site_category']);
$getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
echo '<div class="dragbox" id="item'.$id.'">';
echo '<h2 class="h2handle">'.$site_name.' <span class="close"><img src="assets/img/closepanel.png"></span></h2>';
echo '<div class="dragbox-content" ';
if($user_site_0['collapsed']==1)
echo 'style="display:none;" ';
echo '>';
echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
echo '<p>' . $site_category . '</p>';
echo '<p>' . $site_address . '</p>';
echo '<p>' . $site_desc . '</p>';
echo' </div>
</div>';
}
}
echo '</div>';
?>
<?php
echo '<div class="column grid_3 clearfix" id="column1" >';
echo ' ';
$user_sites_1=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='1' ORDER BY sort_no");
if(!$user_sites_1) {
echo '';
}
else {
while($user_site_1=mysqli_fetch_array($user_sites_1))
{
$id = stripslashes($user_site_1['id']);
$site_name = stripslashes($user_site_1['site_name']);
$site_address = stripslashes($user_site_1['site_address']);
$site_desc = stripslashes($user_site_1['site_desc']);
$site_category = stripslashes($user_site_1['site_category']);
$getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
echo '<div class="dragbox" id="item'.$id.'">';
echo '<h2 class="h2handle">'.$site_name.' <span class="close"><img src="assets/img/closepanel.png"></span></h2>';
echo '<div class="dragbox-content" ';
if($user_site_1['collapsed']==1)
echo 'style="display:none;" ';
echo '>';
echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
echo '<p>' . $site_category . '</p>';
echo '<p>' . $site_address . '</p>';
echo '<p>' . $site_desc . '</p>';
echo' </div>
</div>';
}
}
echo '</div>';
?>
<?php
echo '<div class="column grid_3 clearfix" id="column2">';
echo ' ';
$user_sites_2=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='2' ORDER BY sort_no");
if(!$user_sites_2) {
echo '';
}
else {
while($user_site_2=mysqli_fetch_array($user_sites_2))
{
$id = stripslashes($user_site_2['id']);
$site_name = stripslashes($user_site_2['site_name']);
$site_address = stripslashes($user_site_2['site_address']);
$site_desc = stripslashes($user_site_2['site_desc']);
$site_category = stripslashes($user_site_2['site_category']);
$getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
echo '<div class="dragbox" id="item'.$id.'">';
echo '<h2 class="h2handle">'.$site_name.' <span class="close"><img src="assets/img/closepanel.png"></span></h2>';
echo '<div class="dragbox-content" ';
if($user_site_2['collapsed']==1)
echo 'style="display:none;" ';
echo '>';
echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
echo '<p>' . $site_category . '</p>';
echo '<p>' . $site_address . '</p>';
echo '<p>' . $site_desc . '</p>';
echo' </div>
</div>';
}
}
echo '</div>';
?>
<?php
echo '<div class="column grid_3 clearfix" id="column3">';
echo ' ';
$user_sites_3=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='3' ORDER BY sort_no");
while($user_site_3=mysqli_fetch_array($user_sites_3))
{
$id = stripslashes($user_site_3['id']);
$site_name = stripslashes($user_site_3['site_name']);
$site_address = stripslashes($user_site_3['site_address']);
$site_desc = stripslashes($user_site_3['site_desc']);
$site_category = stripslashes($user_site_3['site_category']);
$getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
echo '<div class="dragbox" id="item'.$id.'">';
echo '<h2 class="h2handle">'.$site_name.' <span class="close"><img src="assets/img/closepanel.png"></span></h2>';
echo '<div class="dragbox-content" ';
if($user_site_3['collapsed']==1)
echo 'style="display:none;" ';
echo '>';
echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
echo '<p>' . $site_category . '</p>';
echo '<p>' . $site_address . '</p>';
echo '<p>' . $site_desc . '</p>';
echo' </div>
</div>';
}
echo '</div>';
?>
The code looks a right state.. Could I do it better?
Use for and change the code, some code :
<?php
for ($i=0; $i<4; $i++)
{
echo '<div class="column grid_3 clearfix" id="column'.$i.'" >';
echo ' ';
$user_sites=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='".$i."' ORDER BY sort_no");
if(!$user_sites) {
echo 'No sites added, please <a class="addsite" href="#">add one now</a>';
}
else
{
while($user_site=mysqli_fetch_array($user_sites))
{
... // Do it yourself
maybe
function get_site($column_id){
$str = "";
$user_sites = mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='".$column_id."' ORDER BY sort_no");
if(!$user_sites_0) {
echo 'No sites added, please <a class="addsite" href="#">add one now</a>';
}
else {
while($user_site_0=mysqli_fetch_array($user_sites_0))
{
$id = stripslashes($user_site_0['id']);
$site_name = stripslashes($user_site_0['site_name']);
$site_address = stripslashes($user_site_0['site_address']);
$site_desc = stripslashes($user_site_0['site_desc']);
$site_category = stripslashes($user_site_0['site_category']);
$getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
$str.= '<div class="dragbox" id="item'.$id.'">';
$str.= '<h2 class="h2handle">'.$site_name.' <span class="close"><img src="assets/img/closepanel.png"></span></h2>';
$str.= '<div class="dragbox-content" ';
if($user_site_0['collapsed']==1)
echo 'style="display:none;" ';
$str.= '>';
$str.= '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
$str.= '<p>' . $site_category . '</p>';
$str.= '<p>' . $site_address . '</p>';
$str.= '<p>' . $site_desc . '</p>';
$str.=' </div>
</div>';
}
}
$str.='</div>';
return $str;}
echo '<div class="column grid_3 clearfix" id="column0">';
echo ' ';
echo oget_site(0);
echo '<div class="column grid_3 clearfix" id="column1">';
echo ' ';
echo oget_site(1);
//....

Categories