Show div only if mySQL info exists - php

I have a MySQL database storing some basic user info, so: name, email, and two phone numbers. I want to make a page with dynamic php text showing the users info, but in the database only one phone number is required. On the page I have:
<p id="first"> Some non-dynamic text </p>
<p> <?php echo $row_rsCustomer['first_name']; ?> </p>
<p> <?php echo $row_rsCustomer['email']; ?> </p>
<p> <?php echo $row_rsCustomer['phone_one']; ?> </p>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>
<p id="second"> Some non-dynamic text </p>
I dont want the #second to be pushed down if the #phonetwo is empty. I was thinking of using some javascript like this:
if( #phonetwo.innerhtml == ""){
#phonetwo.style.display="none";
But I was wondering if there's a way to do this using php? The javascript solution will work I suppose, but I'm pretty sure I've seen somewhere a more "correct" way to do this, I just don't remember what it was nor can I find it anywhere.

Just wrap the paragraph in a conditional
<?php if(isset($row_rsCustomer['phone_two']) && $row_rsCustomer['phone_two']):?>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>
<?php endif?>

<?php if (!empty($row_rsCustomer['phone_two'])) { ?>
<p id="phonetwo"><?php echo $row_rsCustomer['phone_two'] ?></p>
<?php } ?>

Just follow this pattern anytime you need to hide HTML from PHP:
<? if (your condition) { ?>
Html
<? } ?>
It is much better than doing it in JS since it does't send any data to the browser.

First check is it empty or not
<?php if(!empty($row_rsCustomer['phone_two'])) { ?>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>
<?php } ?>

check if $row_rsCustomer['phone_two'] is not empty and then display your <p> :
<?php if (isset($row_rsCustomer['phone_two'])) { ?>
<p id="phonetwo"><?php echo $row_rsCustomer['phone_two'] ?></p>
<?php } ?>

Related

Echo an image tag with site_url() inside PHP tags

I have a loop in my view that outputs all the content gathered from the database:
<?php foreach($content as $contentRow): ?>
<?php
echo $contentRow->value;
?>
<?php endforeach; ?>
This works fine for HTML strings like:
<h2><strong>Example Text</strong></h2>
however I have some image content that I would like to display and I have tried the following database entries to no avail:
<img src="<?php echo site_url('pathToImage/Image.png'); ?>" alt="Cover">"
<img src="site_url('pathToImage/Image.png')" alt="Cover\">"
I feel like I am missing a step on how to use PHP values in this way.
How do I access the URL of the image and use that to show the image?
Full Code Edit
<?php
$CI =& get_instance();
?>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col-md-2"></div>
<div class="col-md-20">
<!--<form class="form-center" method="post" action="<?php echo site_url(''); ?>" role="form">-->
<!-- <h2 class="">Title</h2>
<h2 class=""SubTitle/h2>-->
<?php echo $this->session->userdata('someValue'); ?>
<!--//<table class="" id="">-->
<?php foreach($content as $contentRow): ?>
<tr>
<td><?php
echo $contentRow->value;
?></td>
</tr>
<?php endforeach; ?>
<!--</table>-->
<!--</form>-->
</div>
<div class="col-md-2"></div>
</div>
</div>
</div><!-- /.container -->
and the values are being read out in $contentRow->value;
I have to verify this, but to me it looks like you are echo'ing a string with a PHP function. The function site_url() is not executed, but simply displayed. You can execute it by running the eval() function. But I have to add this function can be very dangerous and its use is not recommended.
Update:
To sum up some comments: The use of eval() is discouraged! You should reconsider / rethink your design. Maybe the use of tags which are replaced by HTML are a solution (Thanks to Manfred Radlwimmer). Always keep in mind to never trust the data you display, always filter and check!
I'm not going to accept this answer as #Philipp Palmtag's answer helped me out alot more and this is more supplementary information.
Because I'm reading data from the database it seems a sensible place to leave some information about what content is stored. In the same table that the content is stored I have added a "content type" field.
In my view I can then read this content type and render appropriately for the content that is stored. If it is just text I can leave it as HTML markup, images all I need to do is specify the file path and then I can scale this as I see fit.
I have updated my view to something akin to this and the if/else statement can be added to in the future if required:
<?php foreach($content as $contentRow): ?>
<?php if ($contentRow->type != "image"): ?>
<?php echo $contentRow->value; ?>
<?php else: ?>
<?php echo "<img src=\"".site_url($contentRow->value)."\">"; ?>
<?php endif; ?>
<?php endforeach; ?>

How can I mix PHP and HTML code?

I have a while loop and I have a HTML code inside the while loop. Inside this HTML code there is PHP code reading a variable that depends on the condition of the while loop.
This variable should be evaluated by PHP. I wrote the code below to solve this but it doesn't work.
$row[0] depends on the condition of while. My code only outputs an empty space for this variable.
<?php
while ($row = mysql_fetch_row($stt)) {
$html='<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0 <span>likes</span>
</p>
<p>
_____________________
</p>
</div>';
echo $html;
}
?>
If you want to display something row by row, do this:
<?php
while($row = mysql_fetch_row($stt)){
$html="<div class='data'>" . nl2br($row[0]) . "\n<p>comment<a href=''>like</a><a href=''>share</a></p><p>0<span>likes</span></p><p></p></div>";
echo $html;
}
?>
> <?php echo nl2br($row[0]."\n"); ?>
this part should be outside of $html.
The interpreter on server side couldnt run.Accordin to php, this part is not a code block. It is just a text.
I found the answer of my question!!!
This is the solution:
<?php
while($row = mysql_fetch_row($stt)){ ?>
<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0
<span>likes</span>
</p>
<p>
_____________________
</p>
</div>
<?php
}
?>

Render data from PHP DB query in HTML

I am a newbie to PHP but trying to learn it to enhance my programming skillset
So far i have the following PHP code in my page to return some data from my Database:
<?php
//code missing to retrieve my data
while($row = mysql_fetch_array($result))
{
echo '$row['Name']';
}
mysql_close($connection);
?>
This is working in that I can see the names from my database displayed on screen. I have seen as well that I can include html in the echo to format the data. However if I have the html code like below in a jQuery accordion outside my PHP code in the page - how can I dynamically place the Names in the specific h3 tags - so the first name in my table is Joe so that comes back in [0] element of array - is there a way I can reference this from my html code outside the php?
<div id="accordion">
<h3>Joe</h3>
<div>
<p>
Some blurb about Joe
</p>
</div>
<h3>Jane</h3>
<div>
<p>
Some blurb about Jane
</p>
</div>
<h3>John</h3>
<div>
<p>
Some Blurb about John.
</p>
</div>
</div>
Try something like this:
<?php while($row = mysql_fetch_array($result)) { ?>
<h3><?php echo $row['name']; ?></h3>
<div>
<p>Some blurb about Joe</p>
</div>
<?php } ?>
I'm assuming 'Some blurb about Joe' would also have to be replaced by a field in the DB, which you can accomplish in the same manner as the name.
#Gert is correct - the original mysql API is deprecated and should not be used anymore. Look into mysqli or PDO, instead.
add your html in while loop like this
<?php
//code missing to retrieve my data
while($row = mysql_fetch_array($result))
{
?>
<h3><?php echo $row['Name']?></h3>
<div>
<p>
Some blurb about <?php echo $row['Name']?>
</p>
</div>
<?php
}
mysql_close($connection);
?>
Like this :
<div id="accordion">
<?php
while($row = mysql_fetch_array($result))
{
<h3><?php echo $row['Name'] ?></h3>
<div>
<p>
Some blurb about Joe
</p>
</div>
} ?>
</div>

How to remove anchor from active navigation page using PHP?

I am sure this is a fairly simple question to answer, but I am new to PHP, so I was hoping someone could help me solve this problem.
I have a dynamic navigation menu that works really well, but I want to remove the link from the current page in the menu.
Here is my code:
<div id="navigation_menu">
<?
foreach($pagedata->menu as $menuitem){
$class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';
?>
<div id="<?=$menuitem->uri?>" class="<?=$class?>">
<img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
<h1><?=$menuitem->title?></h1>
<h2><?=$menuitem->description?></h2>
<img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
</div>
<?
}
?>
</div>
Any help would be greatly appreciated. Thanks!
UPDATED CODE: (this is what works for me now)
<div id="navigation_menu">
<?
foreach($pagedata->menu as $menuitem){
$class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';
?>
<div id="<?=$menuitem->uri?>" class="<?=$class?>">
<img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
<h1>
<?php if ($menuitem->uri == $requesteduri):?>
<?=$menuitem->title;?>
<?php else: ?>
<?=$menuitem->title?>
<?php endif;?>
</h1>
<h2><?=$menuitem->description?></h2>
<img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
</div>
<?
}
?>
</div>
I don't know what your loop is outputting, but you want to match your page name with the menuitem->uri. So you'd get your page name like.. (Put this outside the loop)
<?php echo base_name($_SERVER['REQUEST_URI']); ?>
find out what your loop is outputting (Put this in the loop):
<?php echo $menuitem->uri; ?>
Then you'd create an if statement to compare the current menuitem in the loop and the page request, this is just an example:
<h1>
<?php if (base_name($_SERVER['REQUEST_URI']) == $menuitem->uri):?>
<?=$menuitem->title?>
<?php else: ?>
<?=$menuitem->title;?>
<?php endif;?>
</h1>
Put a conditional around the anchor text to see if $menuitem->uri is equal to the current page URL, accessible from `$_SERVER['REQUEST_URI'] before outputting the anchor tags.

how to code an if else dynamic statement

I'm having trouble writing an if else statement. Considering that I've been doing so for years in ColdFusion, this make me feel very stupid.
Here's the task.
I need to pull first name, last name, email, co-chair status from a database and return the results. However not everyone has an email, so I need a statement that will include a mailto link for those that have emails, and exclude a mailto link for those that don't.
Here's the code I'm using that includes the mailto link for all.
What adjustments do I need to make?
thanks,
david
<?php do { ?>
<?php echo $row_GetMembers['BACmember_First']; ?> <?php echo $row_GetMembers['BACmember_Last']; ?>
<?php /*START_PHP_SIRFCIT*/ if ($row_GetMembers['BACmember_CoChair']=="Yes"){ ?>
<strong> Co-Chair</strong>
<?php } /*END_PHP_SIRFCIT*/ ?><br />
<?php } while ($row_GetMembers = mysql_fetch_assoc($GetMembers)); ?>
This is the line of code you want to optionally display as a link (split for readability):
<a href="mailto:<?php echo $row_GetMembers['BACmember_Email']; ?>">
<?php echo $row_GetMembers['BACmember_First']; ?>
<?php echo $row_GetMembers['BACmember_Last']; ?>
</a>
You'll want something like this instead:
<?php if (!empty($row_GetMembers['BACmember_Email'])): ?>
<a href='mailto:<?php echo $row_GetMembers['BACmember_Email']?>>
<?php echo $row_GetMembers['BACmember_First']; ?>
<?php echo $row_GetMembers['BACmember_Last']; ?>
</a>
<?php else: ?>
<?php echo $row_GetMembers['BACmember_First']; ?>
<?php echo $row_GetMembers['BACmember_Last']; ?>
<?php endif; ?>
If the email field isn't empty (the ! negates the result, so we're checking if it isn't empty), it prints the first and last name inside an anchor tag. If it is empty, it prints the name without it.
A more elegant solution may be to provide the email address as a separate field or link, as opposed to having a list of some names that are links and some that aren't.

Categories