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.
Related
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.
i have made a form using php this is my code
<?php
echo "<div class='error_msg'>";
echo validation_errors();
echo "</div>";
echo form_open('user_authentication/new_user_registration');
echo form_label('Create Username : ');
echo"<br/>";
echo form_input('username');
echo "<div class='error_msg'>";
if (isset($message_display)) {
echo $message_display;
}
echo "</div>";
echo"<br/>";
echo form_label('Email : ');
echo"<br/>";
$data = array(
'type' => 'email',
'name' => 'email_value'
);
echo form_input($data);
echo"<br/>";
echo"<br/>";
echo form_label('Password : ');
echo"<br/>";
echo form_password('password');
echo"<br/>";
echo"<br/>";
echo form_submit('submit', 'Sign Up');
echo form_close();
?>
the form is supposed register new users
how can i style this form using css given that there is no html in the code.
The echo(); function you are using essentially prints HTML, this HTML can be styled by including a style sheet (Google CSS basics and you will quickly find out how to do this).
I would advise running this script and using the web inspector of your chosen browser to look at the HTML code you have created using your PHP code. Again, if your not familiar with the web inspector Google how to open it on your chosen browser.
Hope this clarifies things a bit :)
while ($r1 = $result->fetch_assoc()) {
echo "<tr>
<td>
<a name='".$r1["case_id"]."'
id='".$r1["case_id"]."'
href='modify.php'>".
$r1["case_id"].
"</a></td>
// code continues...
}
Multiple anchor tags will be created at run time and unique name and id will be assigned to each of them.
I want to know id or name of the anchor which was clicked to run modify.php.
You can do this -
<td><a name='".$r1["case_id"]."' id='".$r1["case_id"]."' href='modify.php?id=".$r1["case_id"]."'>".$r1["case_id"]."</a></td>
And when clicked get that with php
if(!empty($_GET['id'])) {
echo $_GET['id'];
}
<td><a name="<?php echo $r1['case_id'];?>" id="<?php echo $r1['case_id']?>" href='modify.php?id=<?php echo $r1["case_id"]?>'><?php echo $r1['case_id']?></a></td>
send id using query string and get the is in php page using GET method
<?php if(!empty($_GET['id'])) {
echo $_GET['id'];
}?>
I tried many type of codes but neither worked. Last one that I tried is this : Change content of div when clicking on link/button
I have 2 buttons that need to execute script. One is to get dynamic content on page that is not working. Second is to logout of site and that one is working.
Here is part of my main page code :
<?php
echo '<p></p>';
echo '<p></p>';
echo '<div style="width:300px;float:left;">';
echo '<button type="buton" id="loyaltybut" onclick="loyalty()" style="background:#8C8C8C;width:300px;margin-bottom:1px;margin-top:7px;" href="#">But1</button>';
echo '<button type="buton" id="logoutbut" onclick="logout()" style="background:#8C8C8C;width:300px;margin:1px auto;">But2</button>';
#Loyalty report - script
echo '<script type="text/javascript">';
echo 'function loyalty() {';
echo '$("#loyaltybut").click(function(){';
echo '$("#data").load("localhost/data #article1");';
echo '}';
echo '}';
echo '</script>';
#Logout - script
echo '<script>';
echo 'function logout() {';
echo 'window.location.replace("localhost/logout/");';
echo '}';
echo '</script>';
echo '</div>';
echo '<div id="data" style="width:750px;float:right;">';
echo '</div>';?>
My data script :
<?php
echo '<div id="article1">';
echo '<b>ARTICLE 1</b> This is my first article';
echo '</div>';
?>
What can I do to make it work ?
Thanks in advance
To Work with Jquery you have to import jquery.js
<script src="http://www.mneye.com/scripts/jquery172.js" type="text/javascript"></script>
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>