Working on a website where you can add news, that data gets sent to a database and then converted to json, which is used in an app.
I'm having trouble with filtering the news using the url. I want to be able to filter news by 2 database columns, Type and Region. When I add the news I need to specify which type[can only choose 1] of news it is and in what region[can choose multiple] it goes under. Now when i'm in the app, let's say I want to only see "Emergency" news from "X" region. For that I would check "Emergency" type checkbox and "X" region checkbox.
With my current code I can filter the news by type perfectly, but im having trouble adding an extra filter to only show X region news.
<?php
function turnIntoUTF8($arrayName) {
if (is_array($arrayName)) {
foreach ($arrayName as $k => $v) {
$arrayName[$k] = turnIntoUTF8($v);
}
} else if (is_string ($arrayName)) {
return utf8_encode($arrayName);
}
return $arrayName;
}
$tag = $_GET["tag"];//Tag = type
//Tried adding a new GET here.
$sql = "SELECT * FROM news WHERE
FIND_IN_SET(`tag`, '$tag')//Tried to do another FIND_IN_SET here with &&.
ORDER BY id DESC";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode(turnIntoUTF8($emparray));
mysqli_close($connection);
?>
Currently I can use this url: http://localhost/vvuudised/getNewsJson.php?tag=uudis,teade to show only news that have their type set as "uudis" or "teade. The outcome is the following:
Now if I wanted to I would filter the news to only show lets say "rohuneeme" region news with the url. How can I do that. Thanks and sorry for the bad explanation.
Related
Just like the image above, when the user clicks one of the list on the right side, it highlights the selected one.
$result = mysqli_query($con, "SELECT * FROM contacts") or die(mysqli_error($con));
while($row = mysqli_fetch_array($result)){
$company = $row['eyo_company_name'];
$id = $row['con_id'];
$editLinks .= "\n\t$company<br>";
}
this is how I brought the list out from the database. Would there be any way I could add a class or add b tag on selected one only ?
If I understand correctly, you are trying to highlight one of the links output by PHP. To do so you will need to know which one to highlight.
If you have a GET variable set to the current contact ID, you could do something like this:
while($row = mysqli_fetch_array($result)){
$company = $row['eyo_company_name'];
$id = $row['con_id'];
//let's check if we've pre-selected a company
// and if so, is this the company we selected?
if (true === array_key_exists('CURRENT_COMPANY_ID', $_GET) && $id === $_GET['CURRENT_COMPANY_ID']) {
//we did select this company for this PHP request
// let's make the HTML output custom to highlight the selected company
$editLinks .= "\n\t<strong>$company</strong><br>";
} else {
$editLinks .= "\n\t$company<br>";
}
}
This example expects that your request to the PHP script generating your list of links will include CURRENT_COMPANY_ID=<ID HERE> in the URL.
For example, if the URL for the request that generates your list of links looks like this:
/getMySuperAwesomeList.php
It would need to look like this:
/getMySuperAwesomeList.php?CURRENT_COMPANY_ID=123
Where 123 matches the con_id of the record you want to highlight.
I chose to use array_key_exists() to make sure that the request actually included the CURRENT_COMPANY_ID data in the URL. Here is documentation for that function: https://www.php.net/array_key_exists
I am trying to display a set of offices ordered under a header that reflects the state in which the offices are located, like so:
State
Office
Office
...
State
Office
...
I have beat my head against this one for a while. There is a single table 'offices' in the MySQL db which has each office's State listed under 'office_state'. Here's what I've come up with - but nothing is returned.
// Here I return a list of the distinct States:
$stateQuery = 'SELECT DISTINCT office_state AS state FROM offices';
$stateResult = mysql_query($stateQuery);
// Here I return each office's information:
$officeQuery = 'SELECT office_information, office_state FROM offices';
$officeResult = mysql_query($officeQuery);
// And here's where I'm stuck:
while ($rows = mysql_fetch_assoc($stateResult)) {
// This returns the distinct States:
echo $rows['state'];
// Here is my effort at listing each office within the State:
$rows1 = mysql_fetch_assoc($officeResult);
if ($rows['state'] == $rows1['office_state']) {
echo $rows1['office_information'];
}
}
Any help would be much appreciated.
try this
<?php
// Here I return a list of the distinct States:
$stateQuery = 'SELECT DISTINCT office_state AS state FROM offices';
$stateResult = mysql_query($stateQuery);
// Here I return each office's information:
$officeQuery = 'SELECT office_information, office_state FROM offices';
$officeResult = mysql_query($officeQuery);
// And here's where I'm stuck:
while ($rows = mysql_fetch_array($stateResult)) {
// Here is my effort at listing each office within the State:
while($rows1 = mysql_fetch_array($officeResult)){
if ($rows['state'] == $rows1['office_state']) {
echo $rows1['office_information'];
}
}
}
?>
So I have a tv show website and I created custom lists where you can add whatever tv shows you want.
There is one table called lists (with user_id, list_title) and another table called show_lists which contains (list_id, show_id).
So my PHP is there:
<?php
$findlistsq = $conn->prepare('SELECT * FROM show_lists, shows, lists
WHERE lists.user_id = :user_id AND
lists.list_id = show_lists.list_id AND
shows.id = show_lists.show_id');
$findlistsq->execute(array(':user_id' => 2));
$listscount = $findlistsq->rowCount();
echo $listscount;
$list = array();
while ($listarray = $findlistsq->fetch()) {
$list[$listarray['list_title']][$listarray['name']] = $listarray;
}
?>
<?php
foreach ($list as $key => $show) {
echo $key; //echo title of list
foreach ($show as $key => $value) {
echo $value['name']; //echo tv shows
;
}
}
?>
Basically, I create an array to join a list to its tv shows, display the title of the list and whatever it contains and so on. My question is: I want to display the list only if there is more than 3 shows (at least 3 different $value).
Can anybody tell me how I could do that ? Thanks!!
Edit: Also, there is slight chance that I might have overcomplicated this. Let me know if I did.
Your explanation of your data is not great, but you could try doing this in the SELECT statement, this will be faster than messing with arrays.
$findlistsq = $conn->prepare('SELECT *
FROM show_lists, shows, lists
WHERE
lists.user_id = :user_id AND
lists.list_id = show_lists.list_id AND
shows.id = show_lists.show_id AND
COUNT(DISTINCT shows.id) > 3');
This will count the DISTINCT (unique) show ids and only return you a data set if there are more than 3 DISTINCT show ids.
You will want to look into is_array() and count()
$list = array('show1','show2','show3');
if(is_array($list)){
if(count($list) > 3){
//more than 3 shows found, execute code!
}
}
I've a online book store where I sell books. Here I store data's of the cart on session. When two or more books are selected there may not be all the books available. I can do check only fore one book that is available or not. But I can't make it through when two or more books selected for ordering. What I did for one book is:
$result = mysql_query("SELECT id FROM book_table WHERE id IN ($cart)");
while ($row = mysql_fetch_array($result)) {
$q=mysql_num_rows(mysql_query("SELECT available FROM book_table WHERE id='$row[0]'"));
if($q!=0){
//order goes fore processing.
}
else{
//books not available.
$q2=mysql_fetch_array(mysql_query("SELECT name FROM book_table WHERE id='$row[0]'"));
echo "The book: $q2[0] Not Available";
//If there is more than 1 books in the cart and one or two books of them are not in the stock, its showing the order cant be processed(But I want to show which books aren't available).
}
}
Here $cart is the books cart saved on session like 1,2,3,4.
Now I need to show which books aren't available to buy by redirecting with another page.
Can anyone help me out on this?
make a array and put id and availaibilty as key and values pair and check for availaiblity for each id.
$all_available = true;
$orders = array();
$result = mysql_query("SELECT id, available, name FROM book_table WHERE id IN ($cart)");
while ($row = mysql_fetch_assoc($result)) {
if ($row['available')) {
$orders[] = $row['id'];
} else {
$all_available = false;
echo "The book: " . $row['name'] . " is not available.\n"
}
}
if ($all_available) {
// Process $orders
} else {
// Send redirect
}
You can do like below,
$query=mysql_query("SELECT id,available FROM book_table WHERE id IN ($cart) ");
$books_available_num = mysql_num_rows($query);
$books_order_num = count(explode(",",$cart));//i think there should be better way to do this...
if($books_available_num==0){
//no book is available
}elseif($books_available_num==$books_order_num ){
//all books are available
while($fetch=mysql_fetch_array($query)){
//order done successfully.
}
}else{
//some are available some are not.
while($fetch=mysql_fetch_array($query)){
if($fetch['status']=="available"){
//place in order
}else{
//this is not available
}
}
}
//rest of code. I think now you can do yourself.
I show you method and you can improve yourself. Code may contain errors, and you can improve it.
you can use ajax like without redirecting to other page
$.ajax({
url :'yourpage.php',
type :'post',
sucess : function(data){ alert(data)}
})
and your php code need a bit modification
$result = mysql_query("SELECT id FROM book_table WHERE id IN ($cart)");
$error = array();
while ($row = mysql_fetch_array($result)) {
$q=mysql_num_rows(mysql_query("SELECT available FROM book_table WHERE id='$row[0]'"));
if($q!=0){
//order goes fore processing.
}
else{
//books not available.
$q2=mysql_fetch_array(mysql_query("SELECT name FROM book_table WHERE id='$row[0]'"));
$error[] = "The book: $q2[0] Not Available";
//If there is more than 1 books in the cart and one or two books of them are not in the stock, its showing the order cant be processed(But I want to show which books aren't available).
}
if (!empty(error))
{
// print array as you want
}
else
{
echo "order successfully placed"
}
}
see more about jquery ajax
if you want to do that redirecting to other page the out $error in session.
I dont think any other thing can help you
NOTE : avoid using mysql_* as they are deprecated
This is part of code from my backoffice page. ( is an edit.php page for a user to edit / modify )
// first query to get cats from user table
$query = "select * from user where name='".$_SESSION['username']."' order by id ASC limit 1";
$result=mysql_query($query);
if (mysql_num_rows($result)) {
while($row=mysql_fetch_array($result)){
$cat1 = $row['cat1'];
$cat2 = $row['cat2'];
$cat3 = $row['cat3'];
$cat4 = $row['cat4'];
$cat5 = $row['cat5'];
$cat6 = $row['cat6'];
$cat7 = $row['cat7'];
$cat8 = $row['cat8'];
$cat9 = $row['cat9'];
$cat10 = $row['cat10'];
}
}
// now i want to build 10 select boxes with selected according the user table $cats
// below is what i can build to first box $cat1
// is there a way i can produce this for the 10 select boxes whitout having to make 10 cycles of bellow code
<select name="theme" id="theme">
<?php
$q1 = 'SELECT * FROM cats ORDER BY title ASC';
$r1 = mysql_query($q1);
while( $row = mysql_fetch_array($r1)) {
if ( $cat1 == $row['id'] ) {
print "<option class=\"cssoption\" selected=\"selected\" value=\"".$row['id']."\">".htmlentities($row['title'])."</option>";
}
else {
print "<option class=\"cssoption\" value=\"".$row['id']."\">".htmlentities($row['title'])."</option>";
}
}
?>
</select>
I am not a coder so this might not be effective code.
Hope someone can help me here and understands what i am trying to do.
Many Thanks.
The code is fine. This 10 cycles as you name it is a almost zero cost.
This is the usual way we do it, we fetch sequentialy the records one by one.
In addition it makes no sense to ask not to do the 10 cycles because you are applying an if else condition in the same time, this means that you check every record if the cat id is the same with the row so you need the loop.
On the other hand if for some reason you want to skip some records, you can use the mysql seek function to start fetching from the desired record.
for($i=0;$i<99999;$i++)
(9999*9999);
echo 'This time the cost of this loop was:',(microtime()-$start),' seconds.';
?>