exploded string manipulation [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have already seen examples for string manipulation using "explode" but none of them does my job..
I have an array of exploded strings:
$grand_result = 12,5,7,3,2,1 ;
$grandparent_id = explode(",",$grand_result);
i want to insert some values in db having id = $grand_result except first value returned (12) in this case
$grand_result can have many values i want to fetch only first 7 values..
how can i do that ...thanks in advance

you can use array_slice
$output = array_slice($grandparent_id, 0, 7);

$grand_result = "12,5,7,3,2,1" ;
$grandparent_id = explode(",",$grand_result);
first update
$avoid = grandparent_id[0];
foreach ($grandparent_id as $id) {
if ($avoid != $id) {
//run your update query
}
}
second fetch
$query = "select * from `table_name` where id in (".$grand_result.") limit 7";

Set Limit of explode
$grand_result = 12,5,7,3,2,1 ;
$grandparent_id = explode(",",$grand_result,8);
unset($grandparent_id[7]); or $grandparent_id = array_slice($grandparent_id, 0, 7);

Related

Mysql search - in which don't have dots [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to search MySQL table and get in result in which don't have . (dot). example is below
example1.com
example2.com
example3
example4 com
So i want return, example 3 and 4. So basically i want row which don't have . (dot).
You can just use NOT LIKE:
SELECT column
FROM tablename
WHERE column NOT LIKE '%.%'
You can do that #fthiella suggested, or you can filter your search results in this was, using the stristr PHP function:
<?php
$searchQuery = 'example';
$sql = 'SELECT * FROM yourTable WHERE domain LIKE "%' . mysql_real_escape_string($searchQuery) . '%"';
$query = mysql_query($query);
$total = mysql_num_rows($query);
if($total) {
$data = array();
while($row = mysql_fetch_array($query)) {
if(stristr($row['domain'], '.') !== false) {
$data[] = $row['domain'];
}
}
die('<pre>' . print_r($data, true) . '</pre>');
} else {
die('Nothing was found.');
}
Note that I don't know your database table structure, so this is a psuedo-example.

Get a length of a php's result [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know php at all so sorry for a simple question - I've got the project started by another person so now I'm trying to finish it.
The problem is - I'm making an android app for which I can't make a change without changing a php and this language I don't know.
here's this part :
function getPlaces(){
$result = array();
$sql = "SELECT id, name, short_description, photo_list, selected, recommended, isTOP FROM place
WHERE id IN(SELECT id_place FROM rubric_place WHERE id_rubric IN(SELECT id FROM rubric WHERE name = '".$_REQUEST["rubric"]."')) ORDER BY isTOP DESC";
getConnect();
$query = mysql_query($sql);
if(!$query){
error100();
}else {
$result['code'] = 200;
for ($i = 0; $i < mysql_num_rows($query); $i++) {
$row = mysql_fetch_assoc($query);
$result["places"][$i] = $row;
$sql = "SELECT * FROM rubric WHERE id IN(SELECT id_rubric FROM rubric_place WHERE id_place = ".$row[id].")";
$queryModule = mysql_query($sql);
if($queryModule){
for ($k = 0; $k < mysql_num_rows($queryModule); $k++) {
$rowModule = mysql_fetch_assoc($queryModule);
$result["places"][$i]["rubrics"][$k] = $rowModule;
}
}
$sql = "SELECT SUM(rating)/COUNT(rating) AS rating FROM comment WHERE id_place = ".$row[id];
$queryModule = mysql_query($sql);
if($queryModule){
$rowModule = mysql_fetch_assoc($queryModule);
$result["places"][$i]["rating"] = $rowModule[rating];
}
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
exit();
}
}
what I need is to make another function that returns in $result a value of "length" of places. I know I can find out length on the other end - in Android's app result, but this particular function will be changed so it will return by 20 results only so I need another function that returns length so plz help
To count the number of characters in a json you can do:
$string = json_encode($result, JSON_UNESCAPED_UNICODE);
$length = strlen($string);
You have to first put the json into a variable to count it, before outputting it.
To count the items in an array before you turn it into a json, you can do:
$length = count($array);

Getting the player with the most kills with a specific weapon [mysql] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Im trying to find the best killer (most kills) out of a kills database.
Put the killerId of $important into an array, and compare it with the rest of the killers. Find the best killer with the weapon $important.
How do I do that?
$array = array();
$index = 0;
while($mData = $q->fetch_assoc())
{
$index++;
$arr = explode('with ', $mData['killText']);
$unimportant = array(" (in paintball)", " (in event)");
$important = str_replace($unimportant, "", $arr[1]);
if(empty($important)) { $important = "Suicide"; }
$array[$important]['Kills']++;
$array[$important]['Gun'] = $important;
$query2 = $mysql->query("SELECT * FROM `kills` WHERE `killText` LIKE '%$important%' AND `killerID` = '". $mData['killerID'] ."'") or die($mysql->error);
while($kData = $query2->fetch_assoc())
{
// put the killerId of $important into an array, and compare it with the rest of the killers. Find the best killer with the weapon $important
}
}
A GROUP BY will do the trick:
"SELECT killerID, COUNT(*) FROM kills WHERE killText LIKE '%$important%' GROUP BY killerID;"
You ca just fetch "killerID" and got the killer with most kills of weapon $important:
$kData = $query2->fetch_assoc();
$kData['killerID'];

why auto dupplicate value in my array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I tried to create a shopping basket in my project, but some problem to handle this.
My project basket must allow duplicate value, but my code auto duplicate last insert to array. Why does this happen, and how to solve this auto duplicate?
<?php
if(isset($_SESSION['basket'])){
$point = count($_SESSION['basket']);
echo " point = ".$point;
$value = end((array_keys($_SESSION['basket'])));
$value++;
}else{
$value = 0;
echo " point = 0";
}
if(isset($_GET['id'] , $_GET['meter'] , $_GET['color'])){
$id = $_GET['id'];
$color = $_GET['color'];
$meter = $_GET['meter'];
$selected_product = array($id , $color , $meter);
list($_SESSION['basket'][$value][0],$_SESSION['basket'][$value][1] , $_SESSION['basket'][$value][2]) = $selected_product;
echo "<pre>";
var_dump($_SESSION['basket']);
echo "</pre>";
}
?>
Though its not exactly clear from your question. To remove duplication in array use array_unique()
You also had a typo at
array($_SESSION['basket'][$value][0],$_SESSION['basket'][$value][1] , $_SESSION['basket'][$value][2]) = $selected_product;

Validation for 3 instances [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to put a validation for when you get the same date, with the same time and same venue, it will not insert in the database. It will only insert in the database if 1 instance will return false.
$res_date = $_POST['res_date'];
$res_venue = $_POST['res_venue'];
$res_dur = $_POST['res_duration'];
$qryjay = mysql_query("SELECT * FROM tbl_reservation WHERE res_date='$res_date' AND res_dur = '$res_dur' AND res_venue = '$res_venue'");
if ((mysql_num_rows($qryjay) == $res_date) && ($qryjay == $res_dur) && ($qryjay) == $res_venue){
echo "<script language=javascript>alert('".$res_venue." is not available on ".$res_dur." on ".$res_date."!')</script>";
}
Try to fix your code like this:
<?
$res_date = mysql_real_escape_string($_POST['res_date']);
$res_venue = mysql_real_escape_string$_POST['res_venue']);
$res_dur = mysql_real_escape_string$_POST['res_duration']);
$qryjay = mysql_query("SELECT count(*) as countNbr FROM tbl_reservation WHERE res_date='".$res_date."' AND res_dur = '".$res_dur."' AND res_venue = '".$res_venue."'");
$result = mysql_fetch_assoc($qryjay);
if ($result['countNbr'] > 0)){
echo "<script language=javascript>alert('".$res_venue." is not available on ".$res_dur." on ".$res_date."!')</script>";
}
?>

Categories