Sorry I'm just beginner in PHP MYSQLi.
I want to ask if how to call 2 sql row value here is my code
<h2><?php echo $userRow['agentFname']; ?></h2>
I want to add 'agentLname' beside 'agentFname'
Thank you.
Option 1...
<h2><?php echo $userRow['agentFname'].' '.$userRow['agentLname'] ?></h2>
Option 2...
<h2><?php echo $userRow['agentFname'] ?> <?php echo $userRow['agentLname'] ?></h2>
Then just print it:
<h2>
<?php
echo $userRow['agentFname'];
echo " ";
echo $userRow['agentLname'];
?>
</h2>
Try this
<h2><?php echo $userRow['agentFname'].' '.$userRow['agentLname']; ?></h2>
Related
When an item is chosen on my site, it opens a details page. This is the top of the details page above the html tags:
<?php require_once('dbconnection.php');
mysqli_select_db($conn, $dbname);
$recordID = $_GET['recordID'];
$query_Master_details = "
SELECT *
FROM Master_List
WHERE Master_List.Master_Id = $recordID
";
$Master_details = mysqli_query($conn, $query_Master_details) or die(mysqli_error());
$row_Master_details = mysqli_fetch_assoc($Master_details);
$totalRows_Master_details = mysqli_num_rows($Master_details);
?>
This is the code that makes the body of the page:
<div class="container2">
<div class="category"><h2><?php echo $row_Master_details['Name']; ?></h2></div>
<p><?php echo $row_Master_details['Name']; ?></p>
<p><img src="img/<?php echo $row_Master_details['Img']; ?>" /></p>
<p><?php echo $row_Master_details['Code']; ?></p>
<p><?php echo $row_Master_details['Length']; ?> Characters</p>
<?php
mysqli_free_result($Master_details);
?>
<!-- end .container2 --></div>
What I would like to do is create an if/else statement that will look at the Style_ID of the selected item and determine if the number is > 3. If it is, I want it to choose an item that has a Style_Id of 1, 2, or 3 and the same Length as the item chosen and return a random row in the layout above, skip a few lines and then display the information for the selected item in the layout above. Else if it is < or = 3, then I need it to just display as above.
I have tried using:
<?php
If (Style_ID > 3) {
echo 'Test';
}Else {
<div class="category"><h2><?php echo $row_Master_details['Name']; ?></h2></div>
<p><?php echo $row_Master_details['Name']; ?></p>
<p><img src="img/<?php echo $row_Master_details['Img']; ?>" /></p>
<p><?php echo $row_Master_details['Code']; ?></p>
<p><?php echo $row_Master_details['Length']; ?> Characters</p>
}
?>
<?php
mysqli_free_result($Master_details);
?>
But it doesn't work and has syntax errors. How can I create this if/else statement?
Note: I would appreciate being able to get one setup for all of it, but if not just fixing this part would be a big help right now.
Thanks to #Brad for his responses, I finally got this one figured out. I ended up changing some of my field names and finally figured out where to close the php tags to make this work. Here is what I ended up with:
<div class="container2">
<div class="category"><h2><?php echo $row_master_details['name']; ?></h2></div>
<?php
if ($row_master_details['type_id'] > 3) {
echo "Test";
}else { ?>
<p><?php echo $row_master_details['name']; ?></p>
<p><img src="img/<?php echo $row_master_details['img']; ?>" /></p>
<p><?php echo $row_master_details['item_code']; ?></p>
<p><?php echo $row_master_details['length']; ?> Characters</p>
<?php
mysqli_free_result($master_details);
?>
<?php } ?>
<!-- end .container2 --></div>
For starters, PHP is case-sensitive.
If (...) {
...
}Else {
You'll want to use lower-case if and else.
Next, if Style_ID is an attribute of the record, you'll need to access it like you did the others.
if ($row_Master_details['Style_ID'] > 3) {
I'm new to CakePHP, I was wondering if there is a way to echo information from the database using a foreach loop but only have HTML links on images where the id is 1 & 7. What's the best way of achieving this?
<?php if ( isset($articles) ){ ?>
<?php foreach($articles as $article):?>
<?php echo ($this->Html->image($article['Post']['picture'], array('alt' => 'storyimage', 'class' => 'smallimg'));?>
<h3 class="caps"><?php echo $article['Post']['genre'];?></h3>
<h2><?php echo $article['Post']['story'];?></h2>
<div id="contentbox2">
</div>
<?php endforeach; ?>
<?php } ?>
This is how it looks in the veiw, my database in looks image data is stored like this:
suki-burberry-sq_500_500_90_s_c1.jpg
Would it be best to echo all the data individually without the foreach loop or could I write an if statement?
If you want only to get the 1 & 7 id you can do this, assuming that it is a predefined number. If it is dynamic or changeable you can do that in your controller.
<?php if ( isset($articles) ){ ?>
<?php foreach($articles as $article):?>
<?php if ($article['Post']['id']==1 || if ($article['Post']['id']==7) ): ?>
<?php echo ($this->Html->image($article['Post']['picture'], array('alt' => 'storyimage', 'class' => 'smallimg'));?>
<h3 class="caps"><?php echo $article['Post']['genre'];?></h3>
<h2><?php echo $article['Post']['story'];?></h2>
<div id="contentbox2">
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php } ?>
Best way is to find from your Model only what you needed.
with the condition $conditions=array('Post.id'=>array(1,7));
I should say first of all I'm not a PHP guy, so if anyone can help with this, I'll do my best to understand any suggestions.
I have the following code that accesses an API and outputs some of the data, via PHP wrapper:
$idMovie=11;
$pelinfo = $tmdb_V3->movieDetail($idMovie);
<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>
This works fine, it shows the data for one item. What I need to do though is show the data for many more items. So far I've just repeated the block and changed the $idMovie variable - but this is of course, is not the way to do it.
I think I need to do this:
Set up an array to hold each variable, so $idMovie[12,34,56,78]
Create a loop to go through each variable, and output the data using
my code block above.
If anyone can point me in the right right direction, that would be most helpful.
Thanks
Dave
There's one very useful construct in PHP - foreach:
<?php foreach($idMovies as $idMovie):
$pelinfo = $tmdb_V3->movieDetail($idMovie); ?>
<h1><?php echo $pelinfo['original_title']; ?></h1>
<h2><?php echo $pelinfo['release_date']; ?></h2>
<img src="<?php echo $pelinfo['poster_path']; ?>">
<p><?php echo $pelinfo['overview']; ?></p>
<?php endforeach; ?>
Here I've used so-called 'alternative syntax', useful when PHP snippets are included in HTML template.
Yet, there's more than one way to iterate through this array. For example:
<?php
$idMovies = array(11, 22, 33, 42);
$pelHTMLs = array_map(function($id) use ($tmdv_V3) {
$pelInfo = $tmdv_V3->movieDetail($id);
// perhaps you should check the result here, no?
return <<<HTML
<h1>$pelInfo[original_title]</h1>
<h2>$pelInfo[release_date]</h2>
<img src="$pelInfo[poster_path]" />
<p>$pelInfo[overview]</p>
HTML;
}, $idMovies);
echo implode("\n", $pelHTMLs);
?>
Here I used array_map function to create an array $pelHTMLs, each element of which is some HTML representation of a movie data, related to an id taken from $idMovies array. Then all these parts are just 'joined' into a single string with 'implode' function - and echoed out.
This form is quite often used in PHP 5.3+ environments (when you can supply an anonymous function into array_map and similar list comprehension functions). But it actually can be done in PHP 5.2 too - you just need to extract this part into a separate function (or class method), then give its name (or array with two params - class name and method name) as 'callback' argument.
$idMovie = array(12,34,56,78);
foreach($idMovie as $id){
$pelinfo = $tmdb_V3->movieDetail($id);
echo "<h1> $pelinfo[original_title] </h1>
<h2> $pelinfo[release_date] </h2>
<img src='$pelinfo[poster_path]'>
<p>$pelinfo[overview]</p>";
}
Try using
while($data = $pelinfo){<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>}
This should go through all of it.
From here you should be able to work out your girst question.
Its as simple as your pseudo-code. Here as an implementation:
<?php
$ids = array('12','34','56','78') //array of movie ids
$foreach($ids as $id) : //I'm a huge fan of foreach vs for
$pelinfo = $tmdb_V3->movieDetail($id);
?>
<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>
<?php endforeach; ?>
$idMovieArr=array(11,22,35,...);
foreach ($idMovieArr as $key) {
$idMovie=$idMovieArr[$key];
$pelinfo = $tmdb_V3->movieDetail($idMovie);
<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>
<?php } ?>
something like this
Argh, this code is not pulling through my custom meta.
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
if (!empty($post_meta)) {
?>
<div class='client-testimonial'><?php echo $my_meta['testimonial']; ?></div>
<div class='client-name'><?php echo $my_meta['name']; ?></div>
<?php
}
?>
But the one below works, the only reason I am not using it is because it still shows the speach marks and dash when the fields are left empty in the admin panel
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
echo "<div class='client-testimonial'>". "'".$my_meta['testimonial']."'". "</div>";
echo "<div class='client-name'>". "-" .$my_meta['name']."</div>";
?>
Please help me on why the first code is not echoing the info. I am at the end of my tether!
You're checking if $post_meta is not empty, you don't have a variable named $post_meta
Change:
if (!empty($post_meta))
to
if (!empty($my_meta))
i think u have checked wrong variable.
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
if (isset($my_meta) && !empty($my_meta)) {
?>
<div class='client-testimonial'><?php echo $my_meta['testimonial']; ?></div>
<div class='client-name'><?php echo $my_meta['name']; ?></div>
<?php
}
?>
In my website I am trying to display all the applicants to jobs that a user has posted, basically I want the out put to simimlar too,
Job Title 1
Aplicant Name 1
Aplicant Name 2
Applicant Name 3
Job Title 2
Applicant Name 4
Application Name 5
Basically I want the applications to be gathered under the jobs they applied for, however the out put I am current getting is,
Job Title 1
Application Name 1
Job Title 1
Applicant Name 2
The code I am using to do this foreach loop is as follows
<?php foreach($applications as $a) : ?>
<h3><?php echo $a['jobtitle']; ?></h3>
<li>
<img src="/media/uploads/candidates/<?php echo preg_replace('/(.gif|.jpg|.png)/', '_thumb$1', $a['profile_image']);?>" width="90" height="60"/>
<p><?php echo $a['name']; ?></p>
</li>
<?php endforeach; ?>
We need the data structure to properly answer, but I'm assuming you need a nested foreach...
<? foreach($jobs as $j): ?>
# list jobs
<? foreach($applicants as $a): ?>
<? if ($j['jobtitle'] == $a['jobtitle']): ?>
# list applicants
<? endif; ?>
<? endforeach; ?>
<? endforeach; ?>
Well you are getting that because you are only outputting
$a['jobtitle']
and
$a['name']
Where are the other names stored? Something like this is probably what you want, although that won't work if you copy/paste it as it seems that $applications[x]['name'] is not an array:
<?php foreach($applications as $a) : ?>
<h3><?php echo $a['jobtitle']; ?></h3>
<li>
<img src="/media/uploads/candidates/<?php echo preg_replace('/(.gif|.jpg|.png)/', '_thumb$1', $a['profile_image']);?>" width="90" height="60"/>
<?php foreach($a['name'] as $name)?>
<p><?php echo $name; ?></p>
<?php endforeach; ?>
</li>
<?php endforeach; ?>
I recommend pulling jobtitle and applicants from the database and storing them in a multidimensional array.
$jobs[$jobtitle][] = $applicant_name;
Then looping through this array in your view. I've omitted your image code for clarity.
foreach($jobs as $title=>applicants){
echo $title;
foreach($applicants as $name){
echo $name;
}
}