Displaying a user profile page PHP - php

I'm currently in the process of making a social network. I'm am displaying all registered users with the following code.
Is there a better way of doing this?
<?php
$query = $db->query("SELECT * from members");
while($r = $query->fetch()) {
echo '<div class="col-sm-6 col-md-6 col-lg-3 membersGrid">';
echo '<div class="profileThumb">';
echo '<img width="130" height="130" src="'.$r['profile_pic'].'"/><br>';
echo $r['username'], '<br>';
echo $r['country'], '<br>';
echo '<a class="viewProfile" href="profile.php"><button>View Profile</button></a>';
echo '</div>';
echo '</div>';
}
?>
I'm trying to create a link to the individual profile pages for each users. Currently each link is within the above while loop, however, I'm a little uncertain of how to link to the relevant profile page. I'm guessing I have to append a variable to the URL. Any guidance would be greatly appreciated.
echo '<a class="viewProfile" href="profile.php"><button>View Profile</button></a>';

It would depend on how your profile.php page is handling the GET variable that determines the profile of the person you are showing. Let's say that the variable is called id and that you have a row in your members table also called id (which acts as the unique key), then your anchor tag would look like:
echo '<a class="viewProfile" href="profile.php?id=' . $r['id']. '"><button>View Profile</button></a>';

First retrieve the user_id of the user from the database as you are doing with the query. Then give this id to the profile link as:
echo '<a href="profile.php?userid=' . $user_id . '>Linkname</a>';
Then in profile.php get this variable through:
$id = $_GET['userid'];
This way you can show the relevant user's profile in the profile.php.
Hope you might get the idea to work on.

If you are producing a list of links to user profiles then there are a couple of different options:
You can either append a $_GET variable to the end of your link as previously mentioned by Lloyd
echo '<a href="profile.php?if=' . $r['id'] . '">';
or you can send a $_POST variable; the easiest way to do this would be to create a list of forms:
<?php
$query = $db->query("SELECT * from members");
while($r = $query->fetch()) {
echo '<div class="col-sm-6 col-md-6 col-lg-3 membersGrid">';
echo '<div class="profileThumb">';
echo '<img width="130" height="130" src="'.$r['profile_pic'].'"/><br>';
echo $r['username'], '<br>';
echo $r['country'], '<br>';
echo '<form action="profile.php" method="post">';
echo '<input type="hidden" value="' . $r['id'] . '" />';
echo '<input type="submit" value="View Profile" />';
echo '</form>';
echo '</div>';
echo '</div>';
}
?>
Sorry if I've misread your question, hope this helps.

Related

Getting Value from clicked div's ID - php mysql issue

I am currently beginning in php mysqli connections and such, and I'm working on a real estate website design prototype.
As of now I've managed to create a search form which displays every single house available for purchase listed in the mysql database.
Now my task is that when the user clicks one of the homes it activates another .php form which shows a more detailed page.
I'm trying to figure out the proper way to do this.
I'm guessing when the user clicks the div he wants, the php form should get a unique value from said div to query the database for that specific property but I'm at a loss here.
This is the Code for the listed available homes:
<?php
echo "<div id='parent'>";
echo "<section class='responsive HomeBox filterDiv' id='";
echo $resultados['tipo']."";
echo "'>";
echo "<div class='HomeBoxImg'>";
echo "<img class='img-thumbnail img-responsive' alt='Forest'
src='img/propiedades/";
echo $resultados['nombre']."";
?>
/1.jpg' onerror="this.src='img/default.jpg'">
<?php
echo "</div>";
echo "<div class='HomeBoxInfo'>";
echo "<h1>";
echo $resultados['direccion']."";
echo "<h2>";
echo $resultados['estructura']."";
echo "- ";
echo $resultados['tipo']."";
echo "</h2>";
echo "</h1>";
echo "<hr style='width:100%;'>";
echo "<div class='homeboxdetailboxes'>";
echo "<img src='img/icons/bed.png'' alt='Dormitorios'>";
echo "<h4>Dormitorios</h4>";
echo "<h5>";
echo $resultados['dormitorios']."";
echo "</h5>";
echo "</div>";
echo "<div class='homeboxdetailboxes'>";
echo "<img src='img/icons/rooms.png'' alt='Ambientes'>";
echo "<h4>Ambientes</h4>";
echo "<h5>";
echo $resultados['ambientes']."";
echo "</h5>";
echo "</div>";
echo "<div class='homeboxdetailboxes'>";
echo "<img src='img/icons/toilet.png'' alt='Baños'>";
echo "<h4>Baños</h4>";
echo "<h5>";
echo $resultados['baños']."";
echo "</h5>";
echo "</div>";
echo "<div class='homeboxdetailboxes'>";
echo "<img src='img/icons/price.png'' alt='Precio'>";
echo "<h4>Precio</h4>";
echo "<h5>";
echo $resultados['precio']."";
echo "</h5>";
echo "</div>";
echo "</div>";
echo "</section>";
echo "</div>";
?>
So what I'm missing would be the php form for displaying a whole new page based on an id or something provided by the user clicking on of these items.
You can use Javascript/JQuery here, like you can add following
echo '<div id="parent" class="number_<?php echo $resultados['nombre']; ?>">';
then to get specific elements id you can use:
var unique_id = document.getElementById('#parent').className.split('_')[1] //Javascript
var unique_id = $('#parent').attr('class').split('_')[1]; //Jquery
than you can use Ajax request or you can use javascript to change url of current page like:
window.location.href = 'https://www.some_url/'+unique_id;
or you can even wrap each estate item inside , and inside this form put hidden input element like:
<input type="hidden" name="unique_number" value="<?php echo $resultados['nombre']; ?>">
and hook on click event on Javascript or JQuery to submit the form.

Random banner display using php and mysql

I'm creating my website located here
I'm trying to add a banner that changes every time a page refreshes. I have set up 2 examples in my database called "link 1" and "link 2". I will want to add more as and when I get them.
What I want to happen is this:
I want to display one of the 2 images on my site and when the user refreshes the page, it will select one of the 2 images and this should continue every time the page refreshes.
I'm testing this out in a page called banner.php before I move it to my footer.php and make it live.
I currently have this code In my banner.php page:
<?PHP
include_once('include/connection.php');
// Edit this number to however many links you want displaying
$num_displayed = 1 ;
// Select random rows from the database
global $pdo;
$query = $pdo->prepare ("SELECT * FROM banners ORDER BY RAND() LIMIT $num_displayed");
$query->execute();
// For all the rows that you selected
while ($row = execute($result))
{
// Display them to the screen...
echo "<a href=\"" . $row["link"] . "\">
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>" ;
}
?>
<br /><br /><br />
But I am getting this error code:
Fatal error: Call to undefined function execute() in banner.php on line 13
My connection page is used by other pages so I know it works.
Please can some one help me on what I am doing wrong.
Need any more info then please ask and I will add it to this post.
Thank you.
Kev
replace this
while ($row = execute($result))
with this:
while ($row = $query->fetch())
EDIT
This make it better to read.
while ($row = $query->fetch()) :
// Display them to the screen...
?>
<a href="<?php echo $row['link']; ?>">
<img src="<?php echo $row['image']; ?>" border="0" alt="<?php echo $row['text'];?>">
</a>
<?php endwhile; ?>
<br/>
<br/>
<br/>

Show first 5 items in list, hide/toggle display of others (PHP)

I'd like to show the first 5 names in a list and toggle the display of any additional names as a single block.
I've currently got the names list as an array object though I'm happy to change it to an array if the solution would be simpler with that.
Here's what I have so far which is *in*complete because I don't know how to create the hidden div of names:
PHP
$names_count=0;
echo '<div id='nameList' class='toggler'>';
foreach($names as $name){
echo '<a id='name'.$name->acct_id.'>'.$name->full_name.'</a>';
if($names_count<=4){
echo '</div><!--toggler div-->';
}
else
<div class='namesList' style='display:none'>
//put additional names in hidden div?
</div>
}
$names_count++;
} //endforeach
JS:
UPDATE Sorry for the confusion. This isn't really a javascript question so I deleted that tag but I'm including the following jQuery code snippet for completeness with the PHP
$('.toggler').click(function(){
var id=this.id;
$('#'+id).toggle();
});
PHP
$names_count = 0;
echo '<div id="nameList" class="toggler">';
foreach($names as $name) {
echo '<a id="name' . $name->acct_id . '">' . $name->full_name . '</a>';
if ($names_count == 4) {
echo '</div><div class="hidden">';
}
$names_count++;
}
echo '</div>';
JS
$('.toggler').click(function(){
$(this).next().toggle();
});
CSS
.hidden {
display: none;
}
Here's an example with two while loops.
$names = array('Bob', 'Andy', 'Tim', 'Max', 'Roger', 'John', 'Test');
$nameCount = count($names);
$nameIndex = 0;
echo '<div id="nameList" class="toggler">';
// Show the first 5 names.
while ($nameIndex < min(5, $nameCount)) {
$name = $names[$nameIndex++];
echo '<a id="name' . $name . '">' . $name . '</a>';
}
// Show the remaining names in a hidden div.
if ($nameIndex < $nameCount)
{
echo '<div class="hiddenNames" style="display:none">';
while ($nameIndex < $nameCount) {
$name = $names[$nameIndex++];
echo '<a id="name' . $name . '">' . $name . '</a>';
}
echo '</div>';
}
echo "</div>";
That code produces the following output.
<div id="nameList" class="toggler">
<a id="nameBob">Bob</a>
<a id="nameAndy">Andy</a>
<a id="nameTim">Tim</a>
<a id="nameMax">Max</a>
<a id="nameRoger">Roger</a>
<div class="hiddenNames" style="display:none">
<a id="nameJohn">John</a>
<a id="nameTest">Test</a>
</div>
</div>
It also safe if you have less than 5 names; the script would produce :
<div id="nameList" class="toggler">
<a id="nameBob">Bob</a>
<a id="nameAndy">Andy</a>
<a id="nameTim">Tim</a>
</div>
For the JS, I would probably do something along the lines of :
$('.toggler').click(function(){
$('.hiddenNames').toggle();
});
Even if the code is a bigger, I find it easier to follow and probably easier to maintain in the long run. (Opinion)
Hope this helps!
To make a <div> hidden:
<div style="display: hidden"></div>
Then the jQuery should make it visible with the .toggle() command.

change an image via php dropdown

What I am trying to do is change the image when an item is selected from the drop down. This is part of a form so I cant have the value change. However the option value is the row id, that row would also contain the target for the image. But because the target 'file' is called outside the loop it isn't firing.
I read I have to call it within the loop first but can't get it to work. Could you look at the code below and throw me a hint?
Thanks
<?php
include ("conned-db.php");
$result = mysql_query("SELECT * FROM gallery")
or die(mysql_error());
echo "<select id='gallery_id' name='gallery_id' style='width:200px;' >";
while($row = mysql_fetch_array( $result ))
{
echo '<option value=' . $row['id'] . '>';
echo $row['gallery_name'];
echo '</option/>';
}
echo "</select>";
echo "</td>";
echo "<td colspan='2' rowspan='2'>";
echo '<img src=' .$row['file']. '/></td>';
?>
Try this, I think this is what you are looking for
If you want to do something like this you must use Ajax. Here you go for the link that helps you to understand about Ajax.
http://www.w3schools.com/php/php_ajax_database.asp
Note:
If you wanted it to be only PHP without Javascript, you would have to sacrifice the 'must not refresh' constraint, as the only way to submit the form is by pressing the button, and submitting the content.
This should work too. If the file locations of the images are available at the time you load the page using ajax is not a must. You have to use ajax if you need to query the server again to retrieve the required file location. The following code assumes that you have the location of the images for each item of the dropdown list at the time you load the page.
<select id='gallery_id' name='gallery_id' style='width:200px;'
onchange='document.getElementById("image").src=this.options[this.selectedIndex].title' >
<?php
while($row = mysql_fetch_array( $result ))
{
?>
<option value='<?php echo $row["id"]; ?>' title='<?php echo $row["file"]; ?>'>
<?php echo $row["gallery_name"]; ?>
</option>
<?php
}
?>
<img id="image" />
Here is a short example which implements java scrip and php where i update src of an image based on the id from the select you might want to change with specific src based on that id
<?php
include ("conned-db.php");
$item = $_GET["imageid"];
if ($item == "")
{
$item = 1;
}
$result = mysql_query("SELECT * FROM gallery")
or die(mysql_error());
?>
<select id='gallery_id' name='gallery_id' onChange="window.location='file.php?imageid='+this.value" style='width:200px;' >
<?
while($row = mysql_fetch_array( $result ))
{
echo '<option value=' . $row['id'] . '>';
echo $row['gallery_name'];
echo '</option/>';
}
?>
</select>
</td>
<?
echo "<td colspan='2' rowspan='2'>";
?>
<img src=' <?=$item?> '/></td>

php form: passing on submitted information

if(!isset($_POST['JoinFaction'])) {
echo '<form method="post" action="'.$page_name.'"><dl id="sample" class="dropdown" align="left">'
.'<dt><a href="#"><span>Recruiting Factions ['.$numrows.']</span>'
.'</a></dt><dd><ul>';
while($faction_re = mysql_fetch_array($sql)){
echo '<li><a href="#">'.$faction_re['f_name'].''
.'<span class="value">'.$faction_re['f_id'].'</span></a></li>';
}
echo '</ul></dd></dl></td>'
.'<td class="faction_bgcolour" align="center"><input type="image" '
.'name="JoinFaction" value="JoinFaction" width="54" height="45" '
.'src="images/global/game/faction/join_faction.png" /></td></form>';
//get output as a variable to use in later code
echo $selected_faction = '<span id="result"></span>';
} else { $selected_faction = '<span id="result"></span>';
echo '<div class="faction_text">Joined: '.$selected_faction.'.</div>';
}
when I:
echo $selected_faction;
in the if statement, it gets the right faction value (i'm using custom jquery drop down boxes by the way here).
The problem is that this id is not being passed to the else statement where i can update the database. I'm really not sure how I would pass the id on to the else statement.
echo $selected_faction is blank in the else clause
Any help would be greatly appreciated.
You're going to need to send the content of that div back to your server with AJAX.
You could start learning here: http://api.jquery.com/category/ajax/
I suppose a good starting point might be: http://api.jquery.com/jQuery.post/

Categories