12

I need to get all the values of a dropdown in an array based upon the id of the dropdown using PHPQuery.

The following is the HTML:

<select name="semester" id="semester" class="inputtxt" onChange="javascript:selectSemester(this, this.form);">
    <option value="">-- Select your Semester --</option>
    <option value="2nd" selected>2nd</option>
    <option value="4th" >4th</option>
    <option value="6th" >6th</option>
    <option value="8th" >8th</option>
    <option value="SE1" >SE1</option>
    <option value="SE3" >SE3</option>
    <option value="SE5" >SE5</option>
    <option value="SE7" >SE7</option>
</select>

I tried this:

$semesters = $all['#semester'];

foreach ($semesters as $semester) {
    echo pq($semester)->text();
    echo '<br>';
}

But I get only a single output with all the values concatenated. How do I get each value as separate element in an array?

7
  • you would just iterate over #semester option Commented Jul 20, 2015 at 22:02
  • Could you elaborate? I am doing the same on my code Commented Jul 23, 2015 at 8:29
  • do it in jquery or javascript and post it to PHP .. huh ? Commented Jul 23, 2015 at 8:34
  • Not possible. I am getting the html in PHP using CURL. Commented Jul 23, 2015 at 9:09
  • 1
    The PHPQuery library hasn't been maintained in 4 years. You might want to use more recent alternatives like DOM-Query (github.com/PHPPowertools/DOM-Query). Commented Sep 8, 2015 at 7:59

6 Answers 6

4
+50

This code works fine for me:

// include part...

$ids = array();

$raw = file_get_contents("http://localhost:8000/test.html"); // your url

$doc = phpQuery::newDocument($raw);

phpQuery::selectDocument($doc);

/** @var DOMElement $opt */
foreach (pq('#semester > option') as $opt) {
    $ids[] = ($opt->getAttribute('value'));
}

print_r($ids); // check if the array has the values stored

So result is

Array
(
    [0] => 
    [1] => 2nd
    [2] => 4th
    [3] => 6th
    [4] => 8th
    [5] => SE1
    [6] => SE3
    [7] => SE5
    [8] => SE7
)

BTW, you can use $doc['#semester > option'] instead of pq('#semester > option'), both variants works fine. If you need to omit some option - you'd make filter based on option attributes, like if ($opt->getAttribute('value') != "").

Sign up to request clarification or add additional context in comments.

Comments

4

quick trick before you reinvent the wheel, use simple_html_dom, you can find it in http://sourceforge.net/projects/simplehtmldom/ this class has being extremely useful in the past and you can even modify it to either use it with CURL or a string containing HTML code.

You will be able to search for objects (tags) or IDs and get the contents of the tag, or iterate in more friendly way.

Comments

4

This code should work. Use the target as "#semester option" instead of "#semester".

$semesters = $all['#semester option'];

foreach ($semesters as $semester)
{
  echo pq($semester)->text();
  echo '<br> newline';
}

Comments

3

Its easy , you need to select the selector with id and iterate it using the tag , create an empty array and store the values in it using a foreach iterator.

$semsterr = array();  //empty Array

//use the option tag for iteration
foreach (pq('select#semester option') as $opt) { 
    $semsterr[] = pq($opt) -> text(); 
 // $semsterr[] = pq($opt) -> attr('value'); in case you need the value
}

print_r($semsterr); // check if the array has the values stored

Comments

3

You must check the value before add in array. Like...

$sem = array();

foreach (pq('#semester option') as $opt) { 

     if(pq($opt) -> val() != '')
     {
      $sem[] = pq($opt) -> text(); 
     }
}

print_r($sem);

Good Luck.. ['}

Comments

0

//HTML//

<select name="semester" id="semester" class="inputtxt" onChange="javascript:selectSemester(this, this.form);">
<option value="">-- Select your Semester --</option>
<option value="2nd" >2nd</option>
<option value="4th" >4th</option>
<option value="6th" >6th</option>
<option value="8th" >8th</option>
<option value="SE1" >SE1</option>
<option value="SE3" >SE3</option>
<option value="SE5" >SE5</option>
<option value="SE7" >SE7</option>

//PHP//

$select = $_POST['semester'];
$count = count($select);
$i = 0;
while($i =< $count)
{
  $i++;
  echo $_POST['semester'][$i]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.