Tag Archives: query

Popular posts by comments

<?php
$args = array( ‘post_type’ => ‘movies’,
‘posts_per_page’ => 10,
‘orderby’ => ‘comment_count’,
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo   the_title().'</br>’;
endwhile;
?>

Upload image in php

Image Insert, Update, Delete with allow image extension and save image with rename using time();

Image saved in upload folder and when you delete record, image is also delete from upload directory.

form.php

<form method=”post” enctype=”multipart/form-data”>
id
<input type=”text” name=”id”>
name
<input type=”text” name=”name”>
image
<input type=”file” name=”image”>
<br>
<input type=”submit” name=”submit” value=”submit”>
</form>
<?php

$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);

if(isset($_POST[‘submit’]))
{
$id = $_POST[‘id’];
$name = $_POST[‘name’];

$define_name = “profile_”.time();

$image = $define_name.”.jpg”;

$allowedExts = array(“gif”, “jpeg”, “jpg”, “png”);
if (($_FILES[“image”][“type”] == “image/gif”)
|| ($_FILES[“image”][“type”] == “image/jpeg”)
|| ($_FILES[“image”][“type”] == “image/png”)
|| ($_FILES[“image”][“type”] == “image/pjpeg”))

{
move_uploaded_file($_FILES[‘image’][‘tmp_name’],”upload/”.$define_name.”.jpg”);
echo “inserted”;
$q=’INSERT INTO `img`(`id`,`name`,`image`) VALUES (“‘.$id.'”,”‘.$name.'”,”‘.$image.'”)’;
$r=mysql_query($q);
}
else
{
echo ‘alert(“wrong file extension!”)’;
}
}
include “display.php”;
?>

display.php

<?php
$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);
$q=’SELECT * FROM `img`’;
$r=mysql_query($q);
?>
<table border=”1″>
<tr>
<td>id</td>
<td>name</td>
<td>image</td>
</tr>
<?php
while($row= mysql_fetch_array($r))
{ ?>
<tr>
<td><?php echo $row[‘id’]; ?></td>
<td><?php echo $row[‘name’]; ?></td>
<td><img src=”upload/<?php echo $row[‘image’]; ?>” width=50 height=50></td>
<td><a href=”edit.php?editID=<?php echo $row[‘id’]; ?>”>edit</a></td>
<td><a href=”delete.php?deleteID=<?php echo $row[‘id’]; ?>&image=<?php echo $row[‘image’]; ?>”>delete</a></td>
</tr>
<?php
}
?>
</table>

editt.php

<?php
$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);
if(isset($_REQUEST[‘editID’]))
{
$id = $_REQUEST[‘editID’];
$q = mysql_query(“SELECT * FROM img WHERE id = ‘$id'”)or die(mysql_error());
$r = mysql_fetch_array($q);
}

?>

<form method=”post” enctype=”multipart/form-data”>
id
<input type=”text” name=”id” value=”<?php echo $r[‘id’]; ?>”>
name
<input type=”text” name=”name” value=”<?php echo $r[‘name’]; ?>”>
image
<input type=”file” name=”image” value=”<?php echo $r[‘image’]; ?>”>
<img src=”upload/<?php echo $r[‘image’]; ?>” width=50 height=50> <br>
<a href=”form.php”>display</a>
<input type=”submit” name=”update” value=”update”>
<input type=”hidden” value=”<?php echo $r[‘image’]; ?>” name=”himage” />
</form>
<?php
if(isset($_POST[“update”]))
{
$id=$_POST[‘id’];
$name=$_POST[‘name’];
$image=$_FILES[‘image’][‘name’];
$oldimage = $_REQUEST[‘himage’];
unlink(‘upload/’.$oldimage);
move_uploaded_file($_FILES[‘image’][‘tmp_name’],”upload/”.$_FILES[‘image’][‘name’]);
$q=’UPDATE `img` SET `name`=”‘.$name.'”, `image`=”‘.$image.'” WHERE `id`=”‘.$id.'” ‘;
$r=mysql_query($q) or die (mysql_error());
echo “updated”;
header(“location:form.php”);
}
?>

delete.php

<?php

$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);

if(isset($_REQUEST[‘deleteID’]))
{
$deleteID=$_REQUEST[‘deleteID’];
$image=$_REQUEST[‘image’];

$q=’DELETE FROM `img` WHERE `id` = “‘.$deleteID.'”‘;
$r=mysql_query($q) or die (mysql_error());

unlink(“upload/”.$image);

echo “deleted”;
header(‘location:form.php’);
}
?>

database.sql

— Database: `image`

— ——————————————————–


— Table structure for table `img`

CREATE TABLE IF NOT EXISTS `img` (
`id` int(50) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`image` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=110 ;


— Dumping data for table `img`

INSERT INTO `img` (`id`, `name`, `image`) VALUES
(24, ‘sss’, ‘1.jpg’);

Insert Update Delete Search in php

index.php

<form name=”f1″ method=”post” action=”query.php”>
<table width=”305″ height=”198″ border=”1″>
<tr>
<td width=”112″>User Id</td>
<td width=”177″><input type=”text” id=”uid” name=”uid”></td>
</tr>
<tr>
<td width=”112″>Username</td>
<td width=”177″><input type=”text” id=”uname” name=”uname”></td>
</tr>
<tr>
<td>Password</td>
<td><input type=”password” id=”pass” name=”pass”></td>
</tr>
<td height=”45″ colspan=”3″ align=”center”><input type=”submit” value=”Insert” name=”insert”>
<input type=”submit” value=”Update” name=”update”>
<input type=”submit” value=”Delete” name=”delete”>
<input type=”submit” value=”Display All” name=”displayall”>
<a href=”search.php”>Search</a></td>
</tr>
</table>
</form>

Query.php

<?php
$cn=mysql_connect(“localhost:3306″,”root”,””);
mysql_select_db(“iuds”,$cn);

// ________________________I N S E R T__________________________________________

if(isset($_POST[“insert”]))
{
$uid=$_POST[‘uid’];
$uname=$_POST[‘uname’];
$pass=$_POST[‘pass’];

$q=”INSERT INTO udata values(‘$uid’,’$uname’,’$pass’)”;

mysql_query($q);
echo “INSERTed successfully”;

}

// _______________________________U P D A T E________________________________

if(isset($_POST[“update”]))
{
$uid=$_POST[“uid”];
$uname=$_POST[“uname”];
$pass=$_POST[“pass”];

$q=”UPDATE udata SET uname=’$uname’,pass=’$pass’ WHERE uid=’$uid'”;
mysql_query($q);
echo “UPDATEd successfully”;
}
// ________________________D E L A T E__________________________

if(isset($_POST[“delete”]))
{
$uid=$_POST[‘uid’];
$uname=$_POST[‘uname’];
$pass=$_POST[‘pass’];

$q=”DELETE FROM udata WHERE uid=”.$uid;
mysql_query($q);
echo “DELETEd successfully”;
}
// ___________________D I S P L A Y_________________________________

if(isset($_POST[‘displayall’]))
{
$uid=$_POST[‘uid’];
$uname=$_POST[‘uname’];
$pass=$_POST[‘pass’];

$q=”select * FROM udata”;
$r=mysql_query($q);

echo “<table border=1>”;
echo “<tr><td>User Id</td><td>User name</td><td>Password</td></tr>”;
while($row=mysql_fetch_array($r))
{
echo “<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td><a href=index.php?uid=$uid[0]>back</a></td>
</tr>”;
}
echo “</table>”;
}

// ____________________________S E A R C H D I S P L A Y____________________

$uid=””;
$uname=””;
$pass=””;
if(isset($_GET[‘uid’]))
{
$r=mysql_query(“select * from udata where uid=”.$_GET[‘uid’]);
while($row=mysql_fetch_array($r))
{
$no=$row[0];
$name=$row[1];
$marks1=$row[2];
}
}

?>

Search.php

<form method=”post”>
<table>
<tr>
<td>Search User By Id</td>
<td><input type=”text” name=”uid”></td>
<td><input type=”submit” value=”search” name=”search”></td>
</tr>
</table>
</form>
<?php

$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“iuds”,$cn);

if(isset($_POST[“search”]))
{
$uid=$_POST[‘uid’];
$r=mysql_query(“select * from udata where uid=’$uid'”);
echo $r;

echo “<table border=1>”;
echo “<tr><td>User Id</td><td>User name</td><td>Password</td></tr>”;
while($row=mysql_fetch_array($r))
{
echo”<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td><a href=search.php?uid=$row[0]>select</a></td>
</tr>”;
}
}
echo “</table>”;
?>

Post by custom days

You can also use day, week, month, year in  (1 week ago)

$last_week_news = new WP_Query( array(
‘date_query’ => array(
array(
‘after’ => ‘1 week ago’,
),
),
‘posts_per_page’ => 5,
));
$query = new WP_Query( $last_week_news );

display metabox with array

<?php
$my_query = new WP_Query(‘post_type=listing&posts_per_page=-1’);

/* bathrooms */
$bathrooms=array();
while ($my_query->have_posts()) : $my_query->the_post();
$bathrooms_val =  get_post_meta($post->ID, “_listingpress_bathrooms”,true);
if(!empty($bathrooms_val))
{
$bathrooms[]=$bathrooms_val;
}
endwhile;  wp_reset_query();

$final_bathrooms= array_unique($bathrooms,SORT_REGULAR);

sort($final_bathrooms);
?><select value=””><option>Badroom</option><?php
foreach($final_bathrooms as $bathroom)
{
?><option value=”<?php $bathroom; ?>”><?php echo $bathroom; ?></option><?php
}
?></select>

Search filter by form

Demo
implimented in “http://estategrid.com/find-agent/
This is form on submit button

<form method=”post” action=””  name=”agent” enctype=”multipart/form-data”>
<?php
/* sidebar for agent form*/

//list each role and each user with that role
global $wp_roles;
?><select name=”myagent”><option value=””>Select Agent</option><?php
foreach( $wp_roles->role_names as $role => $name ) {
$name = translate_with_context($name);
if($name == ‘Agent’){
$this_role = “‘[[:<:]]”.$role.”[[:>:]]'”;
$query = “SELECT * FROM $wpdb->users WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = ‘wp_capabilities’ AND meta_value RLIKE $this_role) ORDER BY user_nicename ASC LIMIT 10000″;
$users_of_this_role = $wpdb->get_results($query);
if ($users_of_this_role) {
foreach($users_of_this_role as $user) {
$curuser = get_userdata($user->ID);
$author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
$agent = $curuser->user_nicename ;
echo ‘<option value=”‘.$agent.'”>’.ucfirst($agent).'</option>’;
}
}
}
}
?>
</select>
<?php
global $wpdb;

$argsa =array( ‘role’ => ‘aamrole_53673234ba574 ‘ );
$a = get_users( $argsa );
$argsb =array( ‘role’ => ‘ aamrole_5329589cf0296 ‘ );
$b = get_users( $argsb );
$agentusers = array_merge( $a, $b );
#print_r($agentusers);

foreach($agentusers as $agentuser)
{
$user_id = $agentuser->ID;
$agentlocation = get_user_meta($user_id,’location’,true);
//$agentlocation = $wpdb->get_var(“SELECT meta_value FROM “.$wpdb->usermeta.” WHERE meta_key = ‘location’ and user_id=$user_id”);

if(!empty($agentlocation))
$location_array[]=strtolower($agentlocation);
}
$mylocation_array= array_unique($location_array);
if(!empty($mylocation_array)){
?><select name=”mylocation”>
<option value=””>Select Location</option>
<?php
foreach($mylocation_array as $mylocation=>$value)    {
echo ‘<option value=”‘.$value.'”>’.ucfirst($value).'</option>’;
}?>
</select><?php
}
?>
<input type=”submit” value=”Refine Results” name=”agent”>
<?php
/* end sidebar for widget form*/
?>
</form>

After submit button

<?php /* after submit form */
if (isset($_POST[‘agent’]))
{
$args = “”;
$myagent = $_POST[‘myagent’];
$mylocation = $_POST[‘mylocation’];
if(!empty($myagent) && !empty($mylocation))
{
$args = array(
‘meta_query’ => array(
‘relation’ => ‘AND’,
array(
‘key’     => ‘nickname’,
‘value’   => $myagent,
‘compare’ => ‘LIKE’
),
array(
‘key’     => ‘location’,
‘value’   => $mylocation,
‘compare’ => ‘LIKE’
),
),

);

}else if(!empty($myagent) && empty($mylocation))
{
$args = array(
‘meta_query’ => array(

array(
‘key’     => ‘nickname’,
‘value’   => $myagent,
‘compare’ => ‘LIKE’
),

),

);
}
else if(empty($myagent) && !empty($mylocation))
{
$args = array(
‘meta_query’ => array(

array(
‘key’     => ‘location’,
‘value’   => $mylocation,
‘compare’ => ‘LIKE’
),

),

);
}
if(!empty($args))
{
$users = new WP_User_Query( $args);
$users = $users->get_results();

foreach($users as $user)
{
?>
<div class=”agent-detail”>
<div class=”box”>
<div class=””><?php echo  get_avatar( $user->user_email); ?></div>
<h6 class=”service-title”>Name: <?php echo $user->nickname; ?></h6>
<h6 class=”service-title”>Location: <?php echo $user->location; ?></h6>
<h6 class=”service-title”>Email: <?php echo $user->user_email; ?></h6>
<h6 class=”service-title”>Mobile Phone: <?php echo $user->phone_mobile; ?></h6>
<h6 class=”service-title” style=”margin-bottom: 7px;”>About this Agent:</h6>
<p><?php echo $user->description; ?></p>
</div>
</div>
<?php
}
}
}
else
{
global $wpdb;
$argsa =array( ‘role’ => ‘aamrole_53673234ba574 ‘ );
$a = get_users( $argsa );
$argsb =array( ‘role’ => ‘ aamrole_5329589cf0296 ‘ );
$b = get_users( $argsb );
$agentusers = array_merge( $a, $b );
#print_r($agentusers);
foreach($agentusers as $agentuser)
{
?>
<div class=”agent-detail”>
<div class=”box”>
<div class=”box-img” style=”width:20%; float:left;”>
<?php echo  get_avatar( $agentuser->user_email); ?>
</div>
<div class=”box-data”>
<table border=1>
<tr><td>Name:</td><td><?php echo $agentuser->nickname; ?></td></tr>
<tr><td>Location:</td><td><?php echo $agentuser->location; ?></td></tr>
<tr><td>Email:</td><td><?php echo $agentuser->user_email; ?></td></tr>
<tr><td>Mobile Phone:</td><td><?php echo $agentuser->phone_mobile; ?></td></tr>
</table>
</div>
<div class=”box-desc”>
<br>About this Agent:
<p><?php echo $agentuser->description; ?></p>
</div>
</div>
</div>
<?php
}
} ?>

<?php
/* end submit form*/?>

%d bloggers like this: