PHP MYSQL <?php echo $row[$variable]; ?> - php

I have the following code that does what I want it to do, but the ones with the backslashes <?php echo $row[\'t_id\']; ?>, the <?php echo $row[\'t_type\']; ?> and the <?php echo ucfirst$row[\'t_fn\']).\' \'.ucfirst($row[\'t_ln\']); ?> do not output their respective variable.
Instead, their output is literal like
view_t_profile.php?tutor_id=<?php echo $row['t_id']; ?>&t_type=<?php echo $row['t_type']; ?>
The <?php echo ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']); ?> doesn't show up on the web page like the previous code, but its in the code italicized and in red text. I'm not a programmer/coder, so if someone can correct my code, I sure would appreciate it. Usually, I can figure it out, but on this one, I cannot.
<?php
if($row['t_type'] == 1)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Contact</strong></center>';
echo '<center><strong><font color="#3BB9FF"><?php echo ucfirst$row[\'t_fn\']).\' \'.ucfirst($row[\'t_ln\']); ?></font></strong></center>';
}
if($row['t_type'] == 0)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Sponsor</strong></center>';
echo '<center><strong><font color="#3BB9FF"><?php echo ucfirst$row[\'t_fn\']).\' \'.ucfirst($row[\'t_ln\']); ?></font></strong></center>';
}
?>

You don't use <?php echo ... ?> when you're already in PHP mode and echoing something. Just concatenate the variable.
echo '<center><strong><font color="#3BB9FF">' . ucfirst($row['t_fn']) . ' ' . ucfirst($row['t_ln']) . '</font></strong></center>';
<?php echo ... ?> is used when you're just outputting HTML directly, and you want to insert a bit of PHP. For instance, like this:
if($row['t_type'] == 1)
{ ?>
<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>
<center><strong>Main Contact</strong></center>
<center><strong><font color="#3BB9FF"><?php echo ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']); ?></font></strong></center>
<?php
}

Try this:
<?php
if($row['t_type'] == 1)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Contact</strong></center>';
echo '<center><strong><font color="#3BB9FF">'.ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']).'</font></strong></center>';
}
if($row['t_type'] == 0)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Sponsor</strong></center>';
echo '<center><strong><font color="#3BB9FF">'.ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']).'</font></strong></center>';
}
?>
When you open <?php once, you can't use this tags again, before you close with ?> tag.

Related

How to I fix this if/else tree? PHP

I am trying to program a sub-system for students on my website, I tried using these if/else statements to determine some outputs if the parameters match with the user information they have. So if a user is a student they can contact other people that are students and cant contact other users that are not students, and vice versa. The code I tried is below. The problem is that the code shows the error message and the message buttons at the same time whether or not the user is or is not a student. TIP: This PHP code is in a .phtml file, hence the amount of PHP opening and closing tags.
<?php if($dc['user']['student'] !== 0){ ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == true){ ?>
<div class="user-button user-follow-button"><?php echo Dc_GetFollowButton($dc['popover']['user_id']); ?></div>
<div class="user-button message-button"><?php echo Dc_GetMessageButton($dc['popover']['user_id']); ?></div>
<?php } ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == false) { ?>
<?php echo $dc['lang']['student_contact_warning']; ?>
<?php } ?>
<?php } ?>
<?php if($dc['user']['student'] !== 1){ ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == false){ ?>
<div class="user-button user-follow-button"><?php echo Dc_GetFollowButton($dc['popover']['user_id']); ?></div>
<div class="user-button message-button"><?php echo Dc_GetMessageButton($dc['popover']['user_id']); ?></div>
<?php } ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == true) { ?>
<?php echo $dc['lang']['student_contact_warning']; ?>
<?php } ?>
<?php } ?>
Do yourself a favour and start to use variables for repeated functions, and look up PHP's HereDoc to allow you to simplify your code. I think you'll agree the following is clearer.
The question now is what values you expect in your conditions. Run the following and check that the outputted IsStudent and Student are what you expect.
<?php
$Student=$dc['user']['student'];
$IsStudent=Dc_IsStudent($dc['popover']['user_id']);
$Dc_FollowBut=Dc_GetFollowButton($dc['popover']['user_id']);
$Dc_MessageBut=Dc_GetMessageButton($dc['popover']['user_id']);
echo"IsStudent: $IsStudent<br>";
echo"Student: $Student<br>";
if($Student!=0){
if($IsStudent==true){
echo<<<EOC
<div class="user-button user-follow-button">$Dc_FollowBut</div>
<div class="user-button message-button">$Dc_MessageBut</div>
EOC;
}else{
echo $dc['lang']['student_contact_warning'];
}
}
if($Student!=1){
if($IsStudent==false){
echo<<<EOC
<div class="user-button user-follow-button">$Dc_FollowBut</div>
<div class="user-button message-button">$Dc_MessageBut</div>
EOC;
}else{
echo $dc['lang']['student_contact_warning'];
}
}
?>
<?php
if($dc['user']['student'] != 0){
if(Dc_IsStudent($dc['popover']['user_id']) == true){
echo '<div class="user-button user-follow-button">'. Dc_GetFollowButton($dc['popover']['user_id']) .'</div>';
echo '<div class="user-button message-button">'. Dc_GetMessageButton($dc['popover']['user_id']) .'</div>';
} else {
echo $dc['lang']['student_contact_warning'];
}
} else if($dc['user']['student'] != 1){
if(Dc_IsStudent($dc['popover']['user_id']) == false){
echo '<div class="user-button user-follow-button">'. Dc_GetFollowButton($dc['popover']['user_id']) .'</div>';
echo '<div class="user-button message-button">'. Dc_GetMessageButton($dc['popover']['user_id']) .'</div>';
} else {
echo $dc['lang']['student_contact_warning'];
}
}
?>
Try putting it in one block, rather than 2 seperate blocks. If-else blocks are meant like that. I would prefer using cases here, though i tried cleaning this up a bit for you to see whats going on.
I agree with #jbes in the other awnser that you should use variables, to make things a lot easier to read. i didn't do this, but i totally agree on that part.
A typo can be made quickly if you constantly run only if blocks, with else, you can point to a different actions in the same if, without closing the complete block. Now I don't know if $dc['user']['student'] can only be 0 or 1, but else you can toss away the complete else-if statement and just change it to else to make it more cleaner. Also to check if not 0 leaves open for a lot of interpretation, what if the value is 2 or 48644. it would always trigger both blocks, so you may want to swap it around to check if the value equals 1 or 0.
I hope this points you in the right direction
I would first make the PHP code more clear. Then you will see that you didn't close some tags.
<?php
if ($dc['user']['student'] !== 0) {
if (Dc_IsStudent($dc['popover']['user_id']) == true) {
echo '<div class="user-button user-follow-button">';
echo Dc_GetFollowButton ($dc['popover']['user_id']);
echo '</div>' . "\n";
echo '<div class="user-button message-button">';
echo Dc_GetMessageButton($dc['popover']['user_id']);
echo '</div>' . "\n";
} else {
echo $dc['lang']['student_contact_warning'];
}
}
if ($dc['user']['student'] !== 1){
if (Dc_IsStudent($dc['popover']['user_id']) == false){
echo '<div class="user-button user-follow-button">';
echo Dc_GetFollowButton($dc['popover']['user_id']);
echo '</div>' . "\n";
echo '<div class="user-button message-button">';
echo Dc_GetMessageButton($dc['popover']['user_id']);
echo '</div>' . "\n";
} else {
echo $dc['lang']['student_contact_warning'];
}
}
?>
Now we can start analyzing the code.

PHP Echo Empty Check with Photos

i have issue with my php code .
First i have Database with Photo link then i add this with echo .
I Fill the Database with /upload/photo-1.png or what ever from the User !
<?php echo$photo; ?>
I add this with :
<?php if(!empty($photo)) {echo 'uploads/photodefault.png'; } ?>
you can write like this
<?php if(!empty($photo))
{
echo $photo;
}
?>
Edit 1:
If photo is not empty then it will show link.
Change your code to this
<?php
if(empty($photo)) {
echo "uploads/photodefault.png";
} else {
echo $photo;
}
?>
With an <img> tag example
<img src="<?php if(empty($photo)) { echo "uploads/photodefault.png"; } else { echo $photo; } ?>">
try the following code
if ($photo != ''){
echo $photo;
}else{
echo "uploads/photodefault.png";
}
<?php if(!empty($photo)) { echo $photo; } else { echo 'default/default_large.png'; } ?>
This fixed my issue.

How to Use HTML inside PHP (Show only if value exists)

I have the following code on my ecommerce product description tabs so that I can enter in all the information one field at a time without having to preformat my product descriptions in Excel with HTML.
<?php echo the_field('color');?><br>
<?php echo the_field('product_info');?><br>
<?php echo the_field('product_size');?><br>
<?php echo the_field('product_size2');?><br>
<?php echo the_field('product_size3');?><br>
Unfortunately this isn't helpful because the tags outside of the PHP will leave blank lines in cases when no value exists for these fields. How can I incorporate the tags inside the PHP so that they're used only when a value exists?
I'm clearly new to PHP (and coding in general), so I really appreciate anyone's help. Thank you!
Try:
if(get_field('field_name') != "")
{
echo '<p>' . get_field('field_name') . '</p>';
}
Should work for you
Can you try this:
<?php echo empty(trim(the_field('color'))) ? '' : the_field('color') . '<br>'; ?>
And repeat the same pattern for every field.
Without knowing what your the_field function does and how "heavy" it is, this should avoid unnecessary duplication of the calls to both test and output the result.
<?php if($color = the_field('color')): ?>
<?php echo $color; ?><br>
<?php endif; ?>
<?php if($product_info = the_field('product_info')): ?>
<?php echo $product_info; ?><br>
<?php endif; ?>
<?php if($product_size = the_field('product_size')): ?>
<?php echo $product_size; ?><br>
<?php endif; ?>
<?php if($product_size2 = the_field('product_size2')): ?>
<?php echo $product_size2; ?><br>
<?php endif; ?>
<?php if($product_size3 = the_field('product_size3')): ?>
<?php echo $product_size3; ?><br>
<?php endif; ?>
Edit: Use this:
<?php echo (the_field('color') != '') ? the_field('color') . '<br>' : ''; ?>
<?php echo (the_field('product_info') != '') ? the_field('product_info') . '<br>' : ''; ?>
<?php echo (the_field('product_size') != '') ? the_field('product_size') . '<br>' : ''; ?>
<?php echo (the_field('product_size2') != '') ? the_field('product_size2') . '<br>' : ''; ?>
<?php echo (the_field('product_size3') != '') ? the_field('product_size3') . '<br>' : ''; ?>

If statement to not show div / h2

This for each / if statement displays changes, and then right below it it displays the changes made.
I am trying to write an if statement that tells it not to show the h2 and the #change_box if there are no changes.
Help would be greatly appreciated.
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>
<?if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>
</div>
<?php
if ($changes) { // You'll have to set this variable
//Echo all of your HTML
else {
//Echo the stuff you'd rather show if they didn't change anything
}
?>
To give you an idea of how I'd write your code
<?php
if ($changes) {
echo '<h2 class="changes">Changes:</h2>';
echo '<div id="change_box">';
foreach ($audit['Events'] as $event) {
if ($event['Type'] != 'Comment') {
echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
}
}
echo "</div>"
}
?>
You could wrap your code with an if like
if(count($audit['Events'])>0)
{
//Your code
}
Wny do you open and close php tags every time? Would be better to make everything in PHP and echo whenever you want to print HTML code.
<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";
foreach ($audit['Events'] as $event) {
if ( $event['Type'] != 'Comment') {
echo "<span class=\"field\">".$event['Field']."</span>";
echo $event['Value'];
echo "(".$event['Previous'] .")";
}
}
echo "</div>";
?>
please make space after each <? and make sure you enabled short tags
<?php if(is_array($audit['Events']) && $audit['Events']):?>
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<?php foreach ($audit['Events'] as $event):?>
<?php if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?php echo $event['Field'];?>
</span>:
<?php echo $event['Value']; ?>
<?php echo $event['Previous'];?>
<?php endif;?>
<?php endforeach;endif;?>
</div>

header() not working in a very specific line

I have 2 li tags combined with PHP that is used for dictionary purposes:
<li <?php echo "title='".$this->text['tt-statistics']."'" ;?> ><a <?php if($location == "analytics") {echo 'id="current"'; } ?> href="analytics-dash.php"><?php echo $this->text['statistics'];?></a></li>
<li <?php echo "title='".$this->text['tt-logins']."'" ;?> ><a <?php if($location == "settings") {echo 'id="current"'; } ?> href="settings-producer.php"><?php echo $this->text['manage-logins'];?></a></li>
If I put a header() command right after $this->text['statistics']; (at the end of the first li) Like this:
<li <?php echo "title='".$this->text['tt-statistics']."'" ;?> ><a <?php if($location == "analytics") {echo 'id="current"'; } ?> href="analytics-dash.php"><?php echo $this->text['statistics']; header('Location: http://www.example.com/');?></a></li>
<li <?php echo "title='".$this->text['tt-logins']."'" ;?> ><a <?php if($location == "settings") {echo 'id="current"'; } ?> href="settings-producer.php"><?php echo $this->text['manage-logins'];?></a></li>
The header command works fine.
But If i put it right on the start of the second li, like this:
<li <?php echo "title='".$this->text['tt-statistics']."'" ;?> ><a <?php if($location == "analytics") {echo 'id="current"'; } ?> href="analytics-dash.php"><?php echo $this->text['statistics'];?></a></li>
<li <?php header('Location: http://www.example.com/'); echo "title='".$this->text['tt-logins']."'" ;?> ><a <?php if($location == "settings") {echo 'id="current"'; } ?> href="settings-producer.php"><?php echo $this->text['manage-logins'];?></a></li>
It doesn't work.
I know that header "must be called before any actual output is sent". But I cant understand why or where am I printing the output that mess up the header() between those 2 places.
The very first character of your example is actually output being already sent.
Your output starts before <?php tag.

Categories