...

Hi! I’m Starx

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

Check which user clicked on which link

Question by user1295105

I have few links on my website, how can I record into database which user clicked on which link. I have records of the links into database and users. I have created a table in which there will be userid, linkid. But im not sure how to code this php. Any ideas?

EDIT:

<a hef="page.php?id=27">pagename</a>

the link above goes to a page where the link is counted and it looks for the url into the database and redirects to that page. But i want to see which user clicked it.

Answer by Starx

Most easiest way would be to pass a link-identifier as the URI parameter

An example:

<a href="page.php?id=27&clicked=pagename">pagename</a>

Now you can get what the user clicked by checking $_GET['clicked']


It seems I misunderstood the question

You can do this on your page.php

$id = $_GET['id']; //Get the page id
$userid = $_SESSION['id']; // Get the user id if stored in session

//Do something with the user id

header("location: ..."); //redirect to a different place
exit;
Read more

PHP Japanese echo string becomes question marks

Question by Asitaka

I’m working on a php site that needs both Japanese and English. In most places I have no trouble getting the Japanese to display correctly, but I can’t get the characters to display if I use a function like this:

echo.php:

<?php
function draw(){
echo "日本語";
}
draw();
?>

I get “日本語”

but if I try this :
index.php:

<?php
 some stuff
 include "echo.php";
 draw();
?>

I get “???”.
Is there any way for me to get my echo file to send the data to the first file in a way it can read it?

EDIT:
The website works, and shows Japanese characters correctly, except when it tries to pull a function from a php file
Here’s the code:

<html lang="ja">
<head>  
<title>Running Projects</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<div id="header">
<? php include "layout.php" ?>
</div>
</body>
</html>

Where layout.php is just a file with a list of links, I have this on each page so the links are on every page.
For a different part, I have to use functions.php to get some data from a database and write it to the page, so, I tried putting layout.php into the functions.php and calling it: The English links appeared, but the Japanese links appeared as question marks.

Answer by Stony

You should change your file endocing to UTF-8 and set the header on the website to UTF-8.

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

or in php

header('content-type: text/html; charset=utf-8');

sorry for the mistake.

Answer by Starx

That happens when charset is not defined or is incorrect.

You can use meta tags to define the charsets. Place the following meta tags as needed on the head section of the page, and It will be rendered correctly.

HTML 4

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

HTML 5

<meta charset="utf-8" />
Read more

How to get content of selected file by jQuery + Ajax

Question by user752433

I have a file upload element as hereafter:

<input type="file" id="uploadFile">

I can get the path & name of selected file by jQuery script as hereafter:

var filePath = $("#uploadFile").val();
var fileName = filePath.split('\').pop();

Could you please advise me how to get the file content (i thought it is in object or string format) and send it to server action.

Answer by Starx

First of all, you cannot get the path of an file-input element using $("#uploadFile").val();. It is a restriction by most of the modern browsers for security reasons.

Next, you have to upload the first, read the contents and throw the content back to the page using ajax.

You can use jQuery Form plugin to submit the form and grab the file contents on your backend.

Read more

What is a .dhtml page?

Question by user366106

What is a .dhtml page?

Answer by Starx

DHTML is a TERM describing the art of making dynamic and interactive web pages.

From the Tag excerpt from Stackoverflow [Read]

Dynamic HTML, or DHTML, is an umbrella term for a collection of technologies used together to create interactive and animated web sites by using a combination of a static markup language (such as HTML), a client-side scripting language (such as JavaScript), a presentation definition language (such as CSS), and the Document Object Model.

For more information, You can check out these links:

Read more

How do I name these PHP arrays?

Question by absentx

Lets say I need three arrays, ford, chevy and dodge. Each array has three items:

$ford['engine'] = 'something';
$ford['color'] = 'something';
$ford['price'] = 'something';

$chevy['engine'] = 'something';
$chevy['color'] = 'something';
$chevy['price'] = 'something';

$dodge['engine'] = 'something';
$dodge['color'] = 'something';
$dodge['price'] = 'something';

I can write that out no problem and it doesn’t take too long. But, lets say I need fifty or sixty arrays that I need to make, all with ten to twenty different items. I want to put a variable at the top of each array’s file to denote what the array name will be, but I am not quite clear on the syntax and I am not too familiar with $$ or how I would use that with arrays:

$name = 'ford';

$(I want this to be "$name")['engine'] = 'something';
$(I want this to be "$name")['color'] = 'something';
$(I want this to be "$name")['price'] = 'something';

I have also considered just doing this:

$name = 'ford';

$view[$name.'_engine'] 
$view[$name.'_color'] 
$view[$name.'_price'] 

Can I get some suggestions on the best way to approach this?

Answer by Starx

Write a small function to do that

$cars = array(); //Create an array to hold the values    
function writeArray($car, $var1, $var2, $var3) {
     global $cars;
     $cars[$car] = array('engine' =>  $var1, 'color' => $var2, 'price' => $var2);
}
//Now use it like
writeArray('Chevy', $something, $something, $something);

//If you want to access the array in the form of $ford['engine'] then use this

extract($cars); //This will split the array into small array accessible by model 
Read more

jquery how can i change only link class when this link ic clicked

Question by ahmedsaber111

i have a li items i need to change class of the clicked items as follow

  • remove class current from the default one
  • add class current to the clicked one

my html code here

                <ul class="filter">
                <li><a href="#" title="" class="current">recent</a></li>
                <li><a href="#" title="">top popularity</a></li>
                <li><a href="#" title="">top commented</a></li>
                <li><a href="#" title="">other ...</a></li>

            </ul>

and here is the jquery code to do that job

      $(".filter > li a").click(function(){

      $(".filter li a.current").removeClass("current");
        $(this).addClass("current");
      });

it works perfect otherwise when i clicked on any link else these links it apply that code to it, i need to apply this code only to ul.filter

Answer by nnnnnn

If you want the click processing to occur only within ul.filter then you should update your selector to reflect that:

$("ul.filter > li a")

Rather than

$(".filter > li a")

If you have more than one ul element with the “filter” class and you only want to apply the functionality to one of them then you should give that particular ul an id and change your selector to use the id.

Answer by Starx

Use :not() to omit the current item

   $(".filter > li a").not(".current").on('click', function(){
       if(!$(this).hasClass("current")) {
           $(".filter").find("a.current").removeClass("current");
           $(this).addClass("current");
           alert($(this).html());
       }
   });

Demo

Read more

How do I make bootstrap table rows clickable?

Question by rjurney

I can hack this myself, but I think bootstrap has this capability.

Answer by Terry

Using jQuery it’s quite trivial. v2.0 uses the table class on all tables.

$('.table > tr').click(function() {
    // row was clicked
});

Answer by Starx

May be you are trying to attach a function when table rows are clicked.

var table = document.getElementById("tableId");
var rows = table.getElementsByTagName("tr");
for (i = 0; i < rows.length; i++) {
    rows[i].onclick = functioname(); //call the function like this
}
Read more

php DOM removing the tag (not content)

Question by dr.linux

$mystring="This is mystring. <a href='http://www.google.com'>Google.</a>"; 
$dom = new DOMDocument; 
$dom->loadHTML($mystring); 
$xPath = new DOMXPath($dom); 
$nodes = $xPath->query('//a');
if($nodes->item(0)) { 
    $nodes->item(0)->parentNode->removeChild($nodes->item(0)); 
} 
echo $dom->saveHTML();  

I want to get output:

This is mystring. Google.

But i got just:

This is mystring.

Answer by Tim Cooper

Try the following:

if($nodes->item(0)) {
    $node = $nodes->item(0);
    $node->parentNode->replaceChild(new DOMText($node->textContent), $node); 
} 

Answer by Starx

Or, Use simple techniques to do simple things.

Here is an alternative to strip_tags()

preg_replace('#<a.*?>(.*?)</a>#i', '1', $text)
Read more

Best way to go about making "simple" line graphs with a very light footprint? jquery? canvas?

Question by android.nick

I’m trying to create line graphs like the one in the image below.
Line Graph

It needs to have a very light weight(in kb), and needs to have points that I could hover(for a tooltip about that point, like in the image). I don’t need pie charts or anything like that, just line graphs like above.

I’m just not sure how to go about it best, I don’t know canvas, and i’m assuming that might be pretty complex trying to do what I need with canvas. I know jQuery decently well.

I’m wondering: Is there a very light weight framework/plugin that would allow me to do just the bare essentials like in the image? If not, how would you suggest going about this with jQuery?

All I need are the lines drawn, with points that I could trigger a hover on, I can take care of the tooltip and all that, i’m just trying to figure out how to draw all the lines the match up with the grid, and get the little circle elements in the right position.

Thanks so much.
ps: light weight to me is not more than a few kb, because I want them to be interactive(not just a static image), but i’m not going to have so many of them that I need a huge jquery plugin, just something small.

Also: I’m trying to make it so it’s responsive, and shrinks to fit a phones screen.

Answer by Starx

If you are not planning any manipulation on the graphs. Use a PHP library called pChart

Read more

Need to automate de creation of a html Family Tree

Question by Daniel Novais

I need do automate (is that a word?) the creation of a Family Tree in html. I don’t have many knowledge of this language, so I need a simple code (or guidelines to it) that allows me to create a simple family tree. Just the basic stuff like a box for the Name and DOB of a person and a arrow pointing to its relatives (children, parents and brothers).

Answer by Starx

As the term automate is used here. I suppose you are trying to create the HTML elements without actually having it in the HTML page. So, here is a way you can create a HTML and the tree structure subsequently.

var div = $("<div />")
div.append("<div id='another' />")
div.append("<div id='next' />")
div.appendTo($('body'));

This create a div, puts two other div inside it, and is appended in the body tag.

Read more
...

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