...

Hi! I’m Starx

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

importing a CSV into phpmyadmin

Question by sico87

I have a CSV that looks like this,

candidate_id,show_on_site,first_name,surname,gender,DOB,showdob,Location,height,eyes,hair_colour,hair_length,accents,unions,training,url,visible,availability
,26,urban talent,Alice,Barry,Female,11 Jan 1942,FALSE,Manchester,5'2,Brown,Black,Mid-length,Native Lancashire,Equity,Urban Talent TV & Drama Workshops,Alice-Barry---5.4.06.jpg,Yes,Yes
,29,urban talent,Angela,Murray,Female,12 Oct 1970,FALSE,Manchester,5'7,Brown,Dark Brown,Long,"Native Manchester, others include - Cheshire, RP, Patois, Standard USA",Equity Member,"BA Acting Studies, Arden School of Theatre<br>Urban Talent TV & Drama Workshops",Angela Murray 1_1.jpg,Yes,Yes
,31,urban talent,Christine,Barton-Brown,Female,4 Jun 1947,FALSE,Manchester,5'0,Hazel,Blonde,Mid-length,"Native Manchester, others include - Liverpool, Cockney, Birmingham, West Country, Standard Scottish, Standard Welch, S Irish",,Manchester School of Acting<br>3 Years at David Johnson Acting Workshops,Christine Barton-Brown web 1.jpg,Yes,Yes
,33,urban talent,Davinia,Jokhi,Female,1 Jul 1979,FALSE,Manchester,5'2,Dark Brown,Dark Brown,Long,"Native Manchester, others include - Liverpool, RP, Lancashire, Birmingham, Cockney, Devon, Geordie, West Country, Glasgow, Edinburgh, South African, Standard & Southern US, Persian, Asian, Indian ~ good ear for accents",,"Manchester School of Acting, with Mark Hudson<br>North Cheshire Theatre College, with David Johnson<Oldham Theatre Workshop",Davinia Jokhi web 4.jpg,Yes,Yes

Is it possible to just insert this data into the existing columns in my database, all I can seem to it insert it as a new table which then has columns name A, B, C , D, E etc.

Answer by Starx

Using the LOAD DATA INFILE SQL statement you can import the CSV file, but to update you can’t. However, there is a trick you can use.

  • Create another temporary table to use for the import
  • Load onto this table from the CSC

    LOAD DATA LOCAL INFILE '/file.csv'
    INTO TABLE temp_table
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY 'n'
    (field1, filed2, field3); 
    
  • UPDATE the real table joining the table

    UPDATE maintable
    INNER JOIN temp_table A USING (field1)
    SET maintable.field1 = temp_table.fiedl1
    
Read more

Redirect Page after Login

Question by Dom

I have user login page on my site that currently uses the following code to redirect users to this page – index.php

// Redirect to frontpage
                        header("Location: index.php");
                }

But I would like to redirect the user to this page: books.php

books.php?u='.$_SESSION['uid'].'

I tried

header("Location: books.php?u='.$_SESSION['uid'].'"); 

But that doesn’t work – I assume that I am missing some ‘ or brackets. Can anyone suggest some help.

Answer by Starx

There is a typo error in your code.

header("Location: books.php?u=".$_SESSION['uid']); 
//                            ^ missing matching quote and extra quote at the end
exit(); // As Bazzz said, you should stop the processing after this
Read more

nivo slider – add text to the slider

Question by oliverbj

I am currently using the nivo slider on my homepage, to show different images. It works perfectly. Although, next to the images there are some text, which I would like to follow each picture.

Here is the HTML code:

     <!-- Splash -->
            <div class="splash">
              <div class="splash-content">
<div id="text1">                
<h1>More traffic to your website</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elitectetur adipis. In ultrices malesuada est eu laoreet. Nullam eget tellus ac lacus bibendum egestas et at libero. Vestibulum commodo magna ut suscipit.</p>
</div>                
              </div>

            <div id="slider" class="nivoSlider nivo-right theme-default">
                <img src="images/splash.png" alt="" title="" />
                <img src="images/splash.png" alt="" title="" />
                <img src="images/splash.png" alt="" title="" />
                <img src="images/splash.png" alt="" title="" />

            </div>


        </div><!-- End Splash -->

As you can see in the HTML code, there is the <div id="slider">, which is the nivo slider. Although, I wish to have the <div id="text1"> to follow each picture. Then have a <div id="#text2"> to follow the next image and so on.

The jQuery code to control the nivo slider:

<script type="text/javascript">
$(window).load(function() {
    $('#slider').nivoSlider({
        effect: 'sliceUp', // Specify sets like: 'fold,fade,sliceDown'
    });
});
</script> 

Thanks in advance.

Answer by Starx

Use title attribute in the <img /> to define text to appear as caption in the slider.


Lets see an example:

<div id="slider" class="nivoSlider">
    <img src="images/slide1.jpg" alt="" />
    <img src="images/slide2.jpg" alt="" title="#htmlcaption" />
                                             <!-- ^ This is one way to bring caption -->
    <img src="images/slide3.jpg" alt="" title="This is an example of a caption" />
                                             <!-- ^ This is ANOTHER way -->
</div>

<!-- This part of caption is called from the second image link above see  `#htmlcaption` there -->
<div id="htmlcaption" class="nivo-html-caption">
    <strong>This</strong> is an example of a <em>HTML</em> caption with <a href="#">a link</a>.
</div>

Update

Without hacking the code, you can override the default class to appear as a separate text box.

Find .nivo-caption style and make some changes to it

.nivo-caption { 
       width: 200px; 
       height: 250px; 
       left: -200px; /* same as the width */
 }

Update 2

Here is a demo with your slider working with the workaround i mentioned.

SO far as this question goes, this is where i stop. Hope it helps.

Read more

Refresh single page PHP element using button

Question by Michael C

I have a single page quote generator, it works by holding the quotes in a 2D array and generating a random number to pull from the matching value in the array using PHP every time that the page is refreshed, I want to change this and make it so that only the element is changed when you press a button, reducing the http requests to the server.

I have tried various “onClick” methods but they never seem to work, I’m assuming that this is something that I should use Javascript (Or any js libraries) for. The function that I want to run when the button is pressed is this:

  <?php 
    $max = max(array_map('count', $arrCSV));
    $num = rand(0,$max);
    $quote = $arrCSV[0][$num] . "." ;
    echo $quote;
  ?>

Any help is much, much appreciated.

Answer by Starx

Like, @Dr.Kameleon, this cannot be done with PHP alone.

Using jQuery, you can reload an element like this

$("#yourdiv").load("the/page/from/where/it/should/be/updated.php");

Attach it to the click event of the button

$("button").click(function() {
    $("#yourdiv").load("the/page/from/where/it/should/be/updated.php"); 
});
Read more

how to highlight text that has a list with an image bullet?

Question by Joseph Walker

how to highlight only the (li) list of text that has a image bullet? How can I accomplish this.

           <ul>
                <li> test A </li>
                <li> test B </li>
                <li> test C </li>
           </ul> 

Answer by Starx

list-style-image is set on the ul [Check here] and correct way to define multiple word CSS is not like [moreinfo]

$("ul").each(function(){
    if($(this).css("listStyleImage") != "") {
        $(this).addClass("highlight");
    }
});

Demo

Read more

Displaying db rows through <ul> with equal or less <li>

Question by Nabeel Basheer

The following is a query results stored in $performset.

      $sqlperform = $ilance->db->query("
            SELECT
            q.title_per 
            FROM " . DB_PREFIX . "perform q
            LEFT JOIN " . DB_PREFIX . "perform_answers a ON (q.cid =  a.cid)
            WHERE a.user_id = '" . $res_vendor['user_id'] . "'
        ");
        if ($ilance->db->num_rows($sqlperform) > 0)
        {
            while ($rows = $ilance->db->fetch_array($sqlperform))
            {  
            $perform .='<li>'.$rows['title_per'].'</li>';  
                }
                      $performset .='<ul>'.$perform.'</ul>';
         }      

  echo   

The values i get from particular variable are
Adobe Flex, C++ Builder, C#/.Net, C/C++/Unix, C/C++/Win32SDK, Oracle DBA,
Data Entry,

The out put I have got form this is

Adobe Flex, C++ Builder, C#/.Net, C/C++/Unix, C/C++/Win32SDK, Oracle DBA, 
Data Entry, Oracle DBA, Adobe Flex ,C++ Builder, C#/.Net , Data Entry
C/C++/Unix, C/C++/Win32SDK

But the output I need is

Data Entry    C++ Builder   C/C++/Win32SDK
Oracle DBA    C#/.Net
Adobe Flex    C/C++/Unix

The html that works for this is as such

    <ul>
                            <li>Data Entry</li>
                            <li>Oracle DBA</li>
                            <li>Adobe Flex</li>
                        </ul>
                        <ul>
                            <li>C++ Builder</li>
                            <li>C#/.Net</li>
                            <li>C/C++/Unix</li>
                        </ul>

How can i script it in php to produce such n number of results

Answer by Starx

Use of array_chunk() is perfect in this case.

Take a looks at this example, might looks complicated but might be how you need

    if ($ilance->db->num_rows($sqlperform) > 0)
    {
        $drows = array();

        while($rows = $ilance->db->fetch_array($sqlperform)) {  
           $drows[] = $rows;
        }


        $chunks = array_chunk($drows, 3, true);       
        foreach($chunks as $chunk) {
          $set = "<ul>";
             foreach($chunk as $key => $value) {
                $set .= '<li>'.$value['title_per'].'</li>';  
             }
          $set .= "</ul>";
          echo $set;
        }

      }

TESTED, but For test, I used the following codes, which simulates the situation above

$ar1  = array(1,2,3); //db row
$ar2 = array($ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1);

$drows = $ar2; //collection of db rows
$chunks = array_chunk($drows, 3, true);
foreach($chunks as $chunk) {
  $set = "<ul>";
     foreach($chunk as $key => $value) {
        $set .= '<li>'.$key.$value[0].'</li>';  
     }
  $set .= "</ul>";
  echo $set;
}
Read more

Image border on DIV

Question by Deryn

Is it possible to use an image of width 480px and height 360px and apply it to a div as a border? I know its possible to implement the below css code as a border, but I can not seem to work out how to use an image instead.

border:1px solid blue;

Answer by Starx

Actually, you can do this using a trick. Check out my demo using Google as border-image

Basically, the idea is having two divs.

<div id="borderDiv"><!-- This will serve as the border -->
    <div id="inner">
    </div>
</div>

CSS

#borderDiv {
    padding: 2px; /* This is the width of the border :P */
    background: url("borderimage.png");
}
#inner {
    background: #fff;
}
Read more

Moving two div elements at once

Question by MySQL

Not sure if I’m being too clear with the title. Sorry, it’s late and I don’t think I have a cleared mind.

Anyway what I want this to do is..

  1. When the div(manager) is clicked, the div(managerp) appears.
  2. The div(manager) moves to the area the same time managerp moves there. Almost as though they move as one div element.
  3. On the next click managerp Fades out, and manager moves back to its original position.

I’m able to do all of that, but it won’t return to it’s original position. I am a novice when it comes to jQuery, so this is where I ask for your help.

EDIT: The div manager doesn’t seem to return to the old position. I’ve tried editing the css through jQuery to get it to, but it doesn’t seem to work.

HTML :

    <div id="manager">
        <span>&raquo;</span>
</div>
<div id="managerp" style="">

</div>

No, I can’t put managerp within manager, it doesn’t seem to position itself as good as it should.

jQuery :

            $(document).ready(function() {
            $("#manager").click(function() {
                $("#manager").css({'margin-left' : '270px', 'border-left' : '0'});
                $("#managerp").toggle("slow", function() {
                    if ($("#managerp").is(':visible'))
                        $("span").html("&laquo;");
                    else 
                        $("span").html("&raquo;");
                });
            });
        });

This should help you get the picture of what it’s supposed to look like. (Sorry if I am giving way too much information)

One
Two

Thank you for your help. If you need me to be more specific as to what it should be doing, just ask.

This is how it looks when it doesn't return correctly.

Answer by Derek

How about this:

$(document).ready(function() {
        $("#manager").click(function() {
            $("#managerp").toggle("slow", function() {
                if($("#managerp").is(':visible')){
                    $("span").html("&laquo;");
                    $("#manager").css({'margin-left' : '0px', 'border-left' : '0'});
                }else{
                    $("span").html("&raquo;");
                    $("#manager").css({'margin-left' : '270px', 'border-left' : '0'});
                }
            });
        });
});

You have to reset the margin to 0px.

Answer by Starx

Use multiple-selector on your code to manipulate multiple elements. Like this

 $("#manager, #managerp").css({
    'marginLeft' : '270px', 
    'borderLeft' : '0'
});
Read more

Find all block elements

Question by Ilya Volodin

I need to find all block elements in a given node. Block elements are not just elements that have display:block in the CSS, but also default block elements like div and p.

I know I can just get computed style of the element and check for the display property, however, my code will execute in a long loop and getting computed styles flushes reflow stack every time, so it will be very expansive.

I’m looking for some trick to do this without getComputedStyle.

Edit

Here’s my current code that I would like to improve:

var isBlockOrLineBreak = function(node)
{
    if (!node) {
        return false;
    }
    var nodeType = node.nodeType;
    return nodeType == 1 && (!inlineDisplayRegex.test(getComputedStyleProperty(node, "display")) || node.tagName === "BR")
        || nodeType == 9 || nodeType == 11;
};

Another edit

jQuery’s .css calls getComputedStyle under the hood. So that’s not what I’m looking for.

My solution

Thanks everyone for suggestions. Unfortunately, none of them matched what I was looking for. After a lot of digging through documentation I realized that there’s no real way to do this without getComputedStyle. However, I came up with the code that should avoid getComputedStyle as much as humanly possible. Here’s the code:

$.extend($.expr[':'], {
    block: function(a) {
        var tagNames = {
            "ADDRESS": true,"BLOCKQUOTE": true,"CENTER": true,"DIR": true,"DIV": true,
            "DL": true,"FIELDSET": true,"FORM": true,"H1": true,"H2": true,"H3": true,
            "H4": true,"H5": true,"H6": true,"HR": true,"ISINDEX": true,"MENU": true,
            "NOFRAMES": true,"NOSCRIPT": true,"OL": true,"P": true,"PRE": true,"TABLE": true,
            "UL": true,"DD": true,"DT": true,"FRAMESET": true,"LI": true,"TBODY": true,
            "TD": true,"TFOOT": true,"TH": true,"THEAD": true,"TR": true
        };
        return $(a).is(function() {
            if (tagNames[this.tagName.toUpperCase()]) {
                if (this.style.display === "block")
                {
                    return true;
                }
                if (this.style.display !== "" || this.style.float !== "")
                {
                    return false;
                }
                else {
                    return $(this).css("display") === "block";
                }
            }
            else {
                if (this.style.display === "block") {
                    return
                }
                else {
                    return $(this).css("display") === "block";
                }
            }
        });
    }
});

Usage of this code is very simple just do $(“:block”) or $(“form :block”). This will avoid using .css property in a lot of cases, and only fallback to it as a last resort.

Starx’s answer was what gave me the idea to do this, so I’m going to mark his message as an answer.

Answer by Starx

The best way I see is to

  • assign a common class to all the not-native block element and
  • using jQuery’s mulitple-selector.

Then we can do it as simple as this this

CSS:

.block { display: block; }

jQuery:

var blockelements = $("div, p, table, ..., .block"); 
                                   // ^ represents other block tags

If you want to include all the block elements. Here is a link

Read more

to get the values from php to jquery using JSON

Question by user1221098

I am very new to these things .. It might be the easiet question but sitll…..I have to get the date from my mysql database using php and from there to jquery file using JSON to make it work with my jquery plugin. I have put the values in array and used `

    $tDate['year']= 2012
    $tDate['month']=03
    $tDate['day']=06


echo json_encode($tDate);

`
and in my jquery I need to read all these data how do I do it .. have been looking into quite a few websites but have no idea

Answer by Starx

Use $.getjSON(), if the requirement is only to get the json data.

$.getJSON('your.php', function(data) {

  $.each(data, function(key, val) {
    alert(val);
  });

});
Read more
...

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