...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
March 13, 2012

mySQL – WHERE, AND/OR selecting

Question by Kolind

I’m trying to select all posts from database, (where an ID is = an id from another table, which connects the people i’m in network with) OR (to select posts where i’m the writer of the post.). Actually i’m trying to get posts from people in my network AND my own.

I’ve found out how to do this, but when using the OR clause it’s selecting the rows twice. So for example, if I write a post it’s duplicated when echo’ed out from database.

My mySQL:

$sId = validate::intValidate($_SESSION['userId']);
$sql = "SELECT * FROM networkPost
            INNER JOIN authentication
                ON networkPost.FK_networkYou=authentication.userId
            INNER JOIN network
                ON networkPost.FK_networkYou=network.networkYou
            WHERE networkMe=$sId AND FK_networkYou=networkYou OR networkYou=$sId
            ORDER BY networkPostId DESC";

networkYou is: The userId of the post writer.

So how do I select my own posts and my networks posts?

Answer by Starx

You have few errors in your code

validate::intValidate($_SESSION['userId']); //missing square bracket

And the WHERE part of the query is missing brackets

WHERE (networkMe=$sId AND FK_networkYou=networkYou) OR networkYou=$sId
Read more

How to add arrow to current page navigation link items?

Question by Jitendra Vyas

Like this. With IE6 compatibility too.

enter image description here

<ul>
<li><a href="/home">Home</a></li>
<li class="selected"><a href="/about/">About</a></li>
<li><a href="/contact-us/">Contact Us</a></li>
<li><a href="/portfolio/">Portfolio</a></li>
<li><a href="/gallery/">Gallery</a></li>
<ul>​

You can use this jsfiddle example to play with

http://jsfiddle.net/jitendravyas/GN6ed/

and this arrow image http://i.imgur.com/QHFqq.gif

Answer by Starx

You can use CSS3’s transform property to rotate a small box to 45 Deg, and make it look like an arrow.

.arrow {
    width: 30px;
    height: 30px;
    background: #000;
    -webkit-transform: rotate(45deg);  /* Saf3.1+, Chrome */
    -moz-transform: rotate(45deg);  /* FF3.5+ */
    -ms-transform: rotate(45deg);  /* IE9 */
    -o-transform: rotate(45deg);  /* Opera 10.5 */
    transform: rotate(45deg);
    filter: progid:DXImageTransform.Microsoft.Matrix(/* IE6–IE9 */
    M11=0.7071067811865476, M12=-0.7071067811865475, M21=0.7071067811865475, M22=0.7071067811865476, sizingMethod='auto expand');
    zoom: 1;
}

Here is a demo

Update 2: Here is the PURE CSS SOLUTION with the border too.

Read more

how to create makefile in javascript?

Question by Navrattan Bansal

Possible Duplicate:
Make File for Javascript

Actually i am writing some javascript for testing purpose.
i want to use multiple javascripts in which functions are defined.
Is there any way to achieve this ?
I think Make file is the way.
But i don’t know that also.
I want to generate make file.
Can any body suggest me how is to be done?

Answer by Starx

If you looking to combine multiple scripts as one. You can the use build script Boilerplate.

Why to use it? Its not only about scripts.

Combines and minifies javascript (via yui compressor)
Inlines stylesheets specified using @import in your CSS
Combines and minifies CSS
Optimizes JPGs and PNGs (with jpegtran & optipng)
Removes development only code (any remaining console.log files, profiling, test suite)
Basic to aggressive html minification (via htmlcompressor)
Autogenerates a cache manifest file (and links from the html tag) when you enable a property in the project config file.
Revises the file names of your assets so that you can use heavy caching (1 year expires).
Upgrades the .htaccess to use heavier caching
Updates your HTML to reference these new hyper-optimized CSS + JS files
Updates your HTML to use the minified jQuery instead of the development version
Remove unneeded references from HTML (like a root folder favicon)
Runs your JavaScript through a code quality tool (optional)
Read more

displaying a message as pop up when my site is open

Question by user1263260

I have a web php web site. I want to show a message on the pop up div when a user browse my site (home page). I need to show the message only the first time with in a browser.
Does anyone know?

Answer by AD7six

There is no php in your question, it is only js and cookies.

The logic you want is:

  • Does cookie <name> exist?
  • yes
    • The user has been to your page/site before
    • do nothing
  • no
    • The user is new
    • write cookie <name> with any value
    • run your “display message” function

Answer by Starx

Basically, you can do this using two step action, using either sessions or cookies

  1. Show the div
    • Check if a parameter is previously set
    • If not show the div
  2. Then set a parameter to confirm it is never again, till the browser is closed

Since other answers are focused on cookies, I will give you an example of session.

session_start();
if(!isset($_SESSION['boxshowed']) || !$_SESSION['boxshowed'])) {
   echo "<div>to show</div>";
   $_SESSION['boxshowed'] = true;
}

Using sessions for this, will show the box again, when the user reopens the site, after completely closing it.

Read more

How to target tablet devices with CSS queries

Question by Dude

I am trying to target tablets with a own css however I do not get it to work. I know there is a way to target iPad specifically but are there no better ways? I want to target Android tablets too but phones such as Samsung Nexus are misinterpreted as a tablet in the way I am doing it now.

Answer by Tarun

you need to use different style sheets according to device resolution

<link rel="stylesheet" media="all" href="style.css">
<link rel="stylesheet" media="screen and (max-device-width: 320px)" href="mobile.css">
<link rel="stylesheet" media="screen and (min-width: 641px) and (max-width: 800px)" href="tablet.css">

Answer by Starx

The best way I think is media queries. And there are lots of scripts, which enable the media queries.

Response.js

It has been also included in modernizr. So, you can use which you prefer.

Read more

Calculate difference between previous and current record in Mysql

Question by Deepak

How to calculate the difference between current and previous record.

Here is the table example

| rid    | time                |    data |
|10000038| 2012-03-13 12:30:18 |     100 |
|10000052| 2012-03-13 12:30:18 |     120 |
|10000053| 2012-03-13 12:30:18 |     140 |
|10000038| 2012-03-13 12:20:18 |     160 |
|10000052| 2012-03-13 12:20:18 |     180 |
|10000053| 2012-03-13 12:20:18 |     160 |
|10000038| 2012-03-13 12:10:18 |     100 |
|10000052| 2012-03-13 12:10:18 |     160 |
|10000053| 2012-03-13 12:10:18 |     160 |

Here I would like to have the result as,

| rid    | time                |    data | DIf |
|10000038| 2012-03-13 12:30:18 |     100 | 0   |
|10000052| 2012-03-13 12:30:18 |     120 | 20  |
|10000053| 2012-03-13 12:30:18 |     140 | 20  |
|10000038| 2012-03-13 12:20:18 |     160 | 20  |
|10000052| 2012-03-13 12:20:18 |     180 | 20  |
|10000053| 2012-03-13 12:20:18 |     160 |-20  |
|10000038| 2012-03-13 12:10:18 |     100 |-60  |
|10000052| 2012-03-13 12:10:18 |     160 | 60  |
|10000053| 2012-03-13 12:10:18 |     160 | 0   |

Note: Look at the table, Every time there is three data dumped in the table with seperate resource id. How to find the difference?

Answer by Starx

Make sure you have a diff column in the table. Then, whenever you are updating the table with new values do this

UPDATE 
   `tablename` 
SET 
    data = 120,
    diff = 120 -
    (
        SELECT prev_data
        FROM (
          SELECT data AS prev_data
          FROM tablename
          WHERE `id` = '1'
        )
       AS prev_data
    )
WHERE 
   id='1';

Tested & WORKS

Demo

Read more

Can I change the WebKit CSS rules in my browser?

Question by Ian K

For the occasional late-night computing session, I like to tone done the colors of the sites I’m using for the sake of retaining my eyesight. My typical go-to extension is Stylish, a useful tool that loads custom CSS in webpages.

Curiosity and tampering led to the question I have here. Can I change WebKit’s CSS defaults in WebKit’s tags to better suit my needs? I’m speaking in regards to custom browser CSS rules, those used in Safari and Chrome for -webkit-, and others for other browsers. Is there any plain-text file that helps define these rules, which would allow me to edit them and therefore tamper with browser-loaded defaults?

Answer by Starx

I don’t recommend going on with this. Making a page appear differently in a certain browser,

  • is not user friendly, but rather confusing
  • and its sure to decrease usability.

Having said that, that are many webkit specific rules which you can define. And, overriding them is the only way of changing the default behavior.

This page consists all the webkit specific rules and you can override these rules by redefining them in your style sheet.

For example:

-webkit-box-orient: vertical;

Override the default rule and orients the elements vertically.

Read more
March 12, 2012

dynamically creating array in php

Question by vaichidrewar

I am trying to create arrays dynamically and then populate them by constructing array Names using variable but I am getting the following warnings

Warning: in_array() expects parameter 2 to be array, null given
Warning: array_push() expects parameter 1 to be array, null given

For single array this method worked but for array of arrays this is not working. How should this be done?

<?php

for ($i = 1; $i <= 23; ++$i) 
{
        $word_list[$i] = array("1"); 
}


for ($i = 1; $i <= 23; ++$i) 
{
  $word = "abc";
  $arrayName = "word_list[" . $i . "]";
  if(!in_array($word, ${$arrayName})) 
  {
    array_push($$arrayName , $word);
  }
}


?>

Answer by Aleks G

Why are even trying to put array name in a variable and then de-reference that name? Why not just do this:

for ($i = 1; $i <= 23; ++$i) 
{
  $word = "abc";
  $arrayName = "word_list[" . $i . "]";
  if(!in_array($word, $word_list[$i])) 
  {
    array_push($word_list[$i] , $word);
  }
}

Answer by Starx

And in the for loop, you are accessing the array the wrong way

Here is your corrected code

for ($i = 1; $i <= 23; ++$i) 
{
  $word = "abc";
  $arrayName = $word_list[$i];
  if(!in_array($word, $arrayName)) 
  {
    array_push($arrayName , $word);
    $word_list[$i] = $arrayName;
  }

}
Read more

two foreach loops combined for email sending

Question by Bowser Entertainment

I am trying to take a two text areas with multiple emails, one with the “from” emails, and one with the “to” emails. Combine the emails line by line and send emails out accordingly.

Ex:

“To” list:

        mike@gmail.com

        nick@hotmail.com

        adam@yahoo.com

“From” list:

        ashley@gmail.com

        brittney@yahoo.com

        racheal@hotmail.com

I want a email sent to:

   mike@gmail.com from ashley@gmail.com

   nick@hotmail.com from brittney@yahoo.com

   adam@yahoo.com from racheal@hotmail.com

Any help would be greatly appreciated. Thanks in advanced.

Below is the script I got so far, It sends to multiple emails from one email.

 <?php
 if (isset($_POST['submit']))
 {

    // Execute this code if the submit button is pressed.
    $raw_email_account = $_POST['email_from'];
    $email = $_POST['email_to'];
    $sent = "";
    $invalid = "";

    //Separating each line to be read by foreach
    $list = explode("n",$email);

    //Rendering the separeted data from each line
    foreach($list AS $data) {
                //Separating each line to be read by foreach
            $item = explode(":",$data);
            $mail_body = '<html><body>email here</body></html>';
                $subject = "subject here";
                $headers  = "From:".$raw_email_account."rn";
                $headers .= "Content-type: text/htmlrn";
                $to = $item[0];

                $mail_result = mail($to, $subject, $mail_body, $headers);

            if ($mail_result) {
               $valid++;
            } else {
                   // write into an error log if the mail function fails
                   $invalid++;
            }
    }

}
?>
<html>
<head>
</head>
<body>
<form action="email_sender.php" method="POST">
<div align="center">From Email Accounts: <textarea name="email_from" cols="100"    rows="60"></textarea></div><br />
<div align="center">To Email Accounts: <textarea name="email_to" cols="100" rows="60">        </textarea></div><br />
<div align="center"><input type="submit" name="submit"></div>
<br>
Valids: <?php echo $valid;?>
<br>
Invalids: <?php echo $invalid;?>
</body>
</html>

Answer by Vitalmax

Add logic that provides same count of $email and $raw_email_account arrays before foreach loop.

$list = explode("n",$email);
$list2 = explode("n", $raw_email_account);

foreach($list AS $key=>$data) {
            ...
            $headers  = "From:".$list2[$key]."rn";
            ...
}

Answer by Starx

If the array is by default, the indexes will coincide. So you can do something like this.

$toList = array(..);
$fromList = array(...);

foreach($toList as $key => $value) {
    $toAddress = $value;
    $fromAddress = $fromList[$key]; 
    //..
    //.. Go on with you mail function
}
Read more

SELECT command in mysql

Question by Jordashiro

I was wondering if there is a way to do something like selecting all without … some columns here

something like SELECT */column1,column2 , is there a way to do this ?
I just need to output something like

column1 , column2 ( from another table ) , here all other columns without column1 ( or something to make the select skip the first few columns)

EDIT:
The thing is that i need this to be dynamic , so i cant just select what i don’t know. I never know how many columns there will be , i just know the 1st and the 2nd column

EDIT: here is a picture http://oi44.tinypic.com/xgdyiq.jpg
I don’t need the second id column , just the last column like i have pointed.

Answer by DanRedux

Oh, so select all but certain fields. You have two options.

One is a little slow.. Copy the table, drop the fields you don’t want, then SELECT *
The other is to build the field list from a subquery to information_schema or something, then remove occurrences of ‘field_i_dont_want’ in that list.

SELECT ( SELECT THE TABLES YOU WANT AND CONCAT INTO ONE STRING ) FROM TABLE

Answer by Starx

If you need to combine records from multiple tables, you need to find a way to relate them together. Primary Keys, Foreign Keys, or anything common among this.

I will try to explain this with a sql similar to your problem.

SELECT table1.id, table2.name, table1.column3, table1.column4 
FROM table1
INNER JOIN table2 On table2.commmonfield = table1.commonfield
Read more
...

Please fill the form - I will response as fast as I can!