PHP MySQL Database multi filtration - php

So I have a table with a whole load of properties and have displayed them on a webpage. As we have a wide range I developed a filtration system. However when used you can only select one tag, but we want to devise it in a way so that you can select one tag and then another. My current code is as follows.
<h3>Advanced Search:</h3>
<table>
<tr>
<td colspan="2" style="vertical-align: top; width:50%; !important;">
<h3>Location</h3>
<div>
Alsancak
Karsiyaka
Lapta
Kayalar
Sadrazamkoy
Cambeli
Baspinar
<!--
-->
</div>
</td>
<td style="vertical-align: top; width:25%;">
<h3>Number of Bedrooms</h3>
<div>
1
2
3
4
</div>
</td>
And it goes on with another few options the code then on the page is as follows:
<h1>Properties</h1>
<?php
$loc = $_GET["loc"];
$con = mysql_connect("localhost","Username","Password");
mysql_selectdb("db",$con);
$sql = "SELECT * FROM Properties WHERE Location='$loc' ORDER BY `Properties`.`Price` ASC";
$mydata = mysql_query($sql,$con);
echo "<h4>Location:" . $loc . "</h4><br>";
while($record = mysql_fetch_array($mydata)){
echo "<div id=\"property\">" . "<img src=\"images/" . $record['MainPic'] . "\" id=\"propimg\" align=\"left\">" . "<div id=\"text\" style=\"text-align: center; margin-right:20px;\"><h2>" . $record['Title'] . "</h2>" . $record['Ref'] . "<br/>" . "£" . $record['Price'] . "<br/>" . $record['Location'] . "<br/>" . "Details</div>" . "<img src=\"images/bottom-line.png\" id=\"bottomline\"></div>" . "</div>" ;
}
mysql_close($con);
?>
Any help would be greatly received. Thanks in advance!

I would suggest using the same page for displaying the results, whether the filter applies to the location or the number of bedrooms. You can always use WHERE (1 = 1) in your SQL so that you can append AND (Location='$loc') or AND (Beds='$bed'), though it would be preferable to use parameters.
Once you have that set up, you can modify your links. Let's say your destination page is webproperty.php. Store that URL in a variable $urlDest.
When you generate your links for filtering by location, add any variables in $_GET that aren't loc to the end of $urlDest, getting something like webproperty.php?bed=2. As you iterate through your locations, append the loc variable to $urlDest, like "<a href=\"$urlDest&loc=$loc\">".
Similarly, when you generate your links for filtering by bedroom count, add any variables in $_GET that aren't bed to the end of $urlDest, getting something like webproperty.php?loc=Alsancak. As you iterate through your bedroom counts, append the bed variable to $urlDest, like "<a href=\"$urlDest&bed=$bed\">".

Related

php while loop echoing element outside of looped content

The forum pages on my website use PHP to create a table and then use a while loop to populate it from the database. This works fine and always has but I have tried to move the anchor, 'link', tag from around the post's title to the entire first section of the post within the table. To do this it goes through the following steps:
Open the table tag [OUTSIDE OF LOOP]
Echo headers [OUTSIDE OF LOOP]
Start WHILE loop that makes another post section for every post found.
Create table row
Create table data
Echo content
Close table data
REPEAT STEPS 5-7 ONCE MORE for post date section
Close table row
close table [OUSTIDE OF LOOP]
It should make the links clickable on all of the first section and they should be within the table like this:
<table> <--- *THIS IS BEFORE THE LOOP, IT GETS RUN ONCE ONLY* -->
<WHILE *do this like 5 times or something*>
<tr>
<a *category link*>
<td>
*content for the 'td' which is taken from the DB*
</td>
<td>
*content for the 'td' which is taken from the DB*
</td>
</a>
</tr>
<ENDWHILE>
</table>
However, in practice they end up outside of the table as can be seen in this screenshot:
Could anyone please explain this and how to fix it?
echo '<table class="forumTable">
<tr>
<th>Category</th>
<th>Last topic</th>
</tr>';
while($row = mysqli_fetch_assoc($catResult)){
echo '<tr>';
echo '<a href="category.php?id=' . htmlspecialchars($row['catID']) . '"><td class="catDesc">';
echo '<h3>' . $row['catName'] . '</h3>' . $row['catDesc'];
echo '</td>';
echo '<td class="catTime">';
$getTops = "SELECT topicID, topicSubject, topicDate, topicCat FROM topics WHERE topicCat = " . $row['catID'] . " ORDER BY topicDate DESC LIMIT 1";
$topResult = mysqli_query($connect, $getTops);
if(!$topResult){
echo '<p style="margin-top: 75px;">The last topic could not be displayed, please try again later.</p>';
}
else{
if(mysqli_num_rows($topResult) == 0){
echo '<p>No topics</p>';
}
else{
while($topRow = mysqli_fetch_assoc($topResult)){
echo '' . $topRow['topicSubject'] . ' at ' . $topRow['topicDate'];
}
}
}
echo '</td></a>';
echo '</tr>';
}
echo '</table>';
Since the source page confirms that the anchors are where you placed them, but the browser moves them around, you can either :
- contain your links inside the td table cell
- use an alternative approach to add the link where you want it html - table row like a link
Did you try to get page not from browser? How it looks?
I think browser does not allow you to put <a> into <table> directly without <tr><td> </td></tr>

Some html tags' parameters messup html/php code

When I create/edit post to my website, and when I use html tags (either type it by my self or using WYSIWYG editor (in this case nicEdit, but I've used different one - same problem)) it does not show anything on the page.
to be more specific: I have article container which imports php code, which than iterates through the db table and echo posts. This is the code:
<h3>Wydarzenia</h3>
<?php
//collects data from table "events" and sort it by "id" date in descending oreder
$event_table_data = mysqli_query($db_con, "SELECT * FROM events ORDER BY id DESC")
or die(mysqli_error());
//puts the data from "events" into an array
$event_content_array = mysqli_fetch_array($event_table_data);
//makes variables from table columns
$id = id;
$title = title;
$happeninng_date = happen;
$created_date = created;
$content = content;
$author = author;
//add new post button
if((isset($_SESSION['sess_wizardrights'])) && ($_SESSION['sess_wizardrights'] == true)){
echo "<a class='e_event_addpost_link' href='./index.php?link=add_post'><button class='e_event_addpost_button' type='button'>Dodaj nowy post</button></a>";
}
do{
//echo each event for main event article
echo "<span class='b_events_span' id='" . $event_content_array[$id] . "'>";
echo " <a class='b_events_anchor' id='" . $event_content_array[$id] . "'></a>";
echo " <h2 class='b_events_title'>" . $event_content_array[$title] . "</h2>";
if((isset($_SESSION['sess_wizardrights'])) && ($_SESSION['sess_wizardrights'] == true)){
echo "<form class='b_events_edit_form' name='edit_post' action='./index.php?link=edit_post' method='post'>";
echo " <input type='hidden' name='id' value='" . $event_content_array[$id] . "'> ";
echo " <input class='e_events_edit_submit' type='submit' value='Edytuj wydarzenie'>";
echo "</form>";
}
echo " <div class='fb-share-button' data-href='http://www.zhppgkaberdeen.co.uk/index.php?link=events#" . $event_content_array[$id] . "'></div>";
echo " <p class='b_events_happening_date'><i>Data wydarzenia: " . $event_content_array[$happeninng_date] . "</i></p>";
echo " <p class='b_events_content'>" . $event_content_array[$content] . "</p>";
echo " <p class='b_events_created_date'>Utworzono: " . $event_content_array[$created_date] . "</p>";
echo " <p class='b_events_author'><b>Czuwaj!</b><br/>" . $event_content_array[$author] . "</p>";
echo "</span>";
}while($event_content_array = mysqli_fetch_array( $event_table_data ));
?>
When I add tags like <img> for instance with alt="" parameter it completely ruins web page. What I mean by that is that the article container is not shown at all (I've checked that with firebug) as well as everything after the article tag (like footer for instance). What is even more strange is that this: alt="text" did not break the page but every other random word does, how ever it does not show the "text" at image at all.
Similar thing happen when I tried to create table within the post and title or target parameter, I cant remember which one.
I have tried to use htmlentities($event_content_array[$content]), htmlspecialchar($event_content_array[$content]) and mysqli_real_escape_string($do_con, $event_content_array[$content]) -> non of this helped.
Is there a way to fix that or I just need to give up using this tags/parameters?
Thank you for any help or explanation
if you are using e.g echo "<img alt="text">";try to escape the " such as alt=\"text\"

How to display comments inline

So I have this code (which works) that prints out the comments a user has just written. The issue im having is with displaying the comments. The username is displayed above the comment and I just want them to be displayed next to each other for example
'Username:'comment''
I have tried putting display:inline; in my CSS for h4 and p but it messes up everything else that i have put into these elements.
Does anyone know the answer?
This is my code
//Print out existing comment
$query = "SELECT * FROM comments JOIN users ON comments.userID = users.ID WHERE salonID=$salonid";
$result = mysqli_query($db_server, $query);
if (!$result) die("Database access failed: " . mysqli_error($db_server));
while ($row = mysqli_fetch_array($result)){
$str_comments .= "<div id='comments'><h4>" . $row['Username'] ."</h4><p>'" . $row['comment'] . "'</p>";
$str_comments .="<img src='" . $row['imagename'] ."' /></div>";
}
You put username inside <h4></h4> which is a block element. It's the same for <p></p>.
Block element will take a full row so you won't be able to place username & comment in a line.
You already give display:inline style for <h4></h4> & <p></p> it should be fine.
I encourage you to use a <span></span> instead which is an inline element by default.
Focus on this:
while ($row = mysqli_fetch_array($result)){
$str_comments .= "<div id='comments'>" .
"<span style='font-weigth:bold;'>" . $row['Username'] ."</span>" .
"<span>'" . $row['comment'] . "'</span>" .
"<img src='" . $row['imagename'] ."' />".
"</div>";
}
The easiest way to do it on your website is use annote ( http://annote.in ). A javascript based inline comment plugin. You can get started on any platform in minutes.

How to submit parameters by hyperlink-url and retrieve them in php?

Carrying values in a while cycle, each value is extracted from a SQL table, and I need to carry em in a since I'm making a list of items purchased from a specific client. I'm using this line for the href url:
echo "<table width=1200 style align=left>";
while($row3 = mysqli_fetch_array($rs3)){
$space = array("");
$text=$row3['description'];
$dash="_";
$url = preg_replace("![^a-z0-9]+!i", "_", $text);
$url= "".$url.".php";
echo "<tr><td colspan=2 style align=center><span class=font>" . $row3['relid'] . "</span></td><td width=132 style align=center><span class=font>" . $row3['invoiceid'] . "</span></td><td width=400 style align=center <span class=font><a href=$url>" . $row3['description'] . "</span></A></td><td width=132 style align=right><span class=font>" . $row3['amount'] . "</span></td><td width=132 style align=center><span class=font>Pendiente</span></td><td width=132 style align=left><span class=font>" . $row3['duedate'] . "</span></td></tr>";
}
mysqli_close($con3);
echo "</table>";
echo"<table width=1200>";
echo " <tr class=font>
<td width=1200 height=22 colspan=7 background=border-green-horizontal.png> </td>
";
I'm generating the url from the Description I extract from the database but I need to carry the Order ID, Client ID, etc from the specific row.
Each variable is needs to be carried to a form where the user will fill some fields regarding the product the client bought (Medical Test Results), and I need those variables to mark as "Done" so It won't show up again on the list.
Fix these syntax errors, report back if still broken.
<td width=400 style align=center <span class=font><a href=$url>" . $row3['description'] . "</span></a></td>
-->
"<td width=400 style align='center'><span class='font'><a href='$url'>" . $row3['description'] . "</span></a></td>"
EDIT:
Do you mean how to append a var to an url, so it can be retrived on the called page?
You can append the var to the url by echo "<a href='{$url}?id={$var}'>.
On the called page, you can read it by $_GET['id']

Get & diplay from database error

I am trying to write a simple stream, streaming everything from a mysql field. But my current script shows absolutely nothing.. no errors, nothing. Here it is:
include("user_sytem_scripts/connect.php");
$sql_updates = mysql_query("SELECT item_id, username, item_content, update_time FROM updates ORDER BY update_time DESC LIMIT 30") or die("Query failed with error: ".mysql_error());
while($row = mysql_fetch_array($sql_updates)){
$update_id = $row["item_id"];
$update_username = $row["username"];
$item_content = $row["item_content"];
$update_time = $row["update_time"];
$updatestream = '
<table style="background-color:#FFF; border:#999 1px solid; border-top:none;" cellpadding="5" width="100%">
<tr>
<td width="90%" valign="top" style="line-height:1.5em;">
<span class="liteGreyColor textsize9">' . $update_time . ' <strong>' . $username . '</strong> via <em></em></span><br />
' . $item_content . '
</td>
</tr></table>'; }
Then down in the HTML I use: <?php echo $updatestream ?>
But as i said i get absolutly nothing.. Can anyone spot any errors or general mistakes that would cause this? Thanks :D
I would check the following:
Does the query run when you run it manually (in phpMyAdmin for example)?
Do you get any output at all? If not, it seems that your while look is getting 0 results from the DB.
You should also change: $updatestream = ' to $updatestream .= so that you append text, in the current while loop you'll overwrite the last $updatestream value.
First you need to initialize $updatestream,($updatestream = ""), before the while loop, then inside the loop you change it to $updatestream .= "the string". You might want to echo your result at the end, sometimes we forget the little stuff.
echo $updatestream;

Categories