How can I use explode function in php

I already implode some data in mysql

// customer table
cust_id, impid, 
1, 1,2

// sports table
sport id, sport name 
1, football
2, basketball

How can I explode and make a php/mysql query

I tried this query but doesn't get the output.

$sql1=mysql_query("SELECT impid FROM customer where c_id=1") or die (mysql_error());
$result1 = mysql_fetch_array($sql1);
echo $exp= $result1['impid']; // get 1, 2 from here 
echo $array=explode(",",$exp); // cannot get the output from explode function, How can I get the sports name from here.

Any one figured out the problem, pls let me know

Any one figured out the problem, pls let me know

First your sql query is not good. You have to have relation between those 2 tables. There are simple example:

$sql1=mysql_query("SELECT * FROM customer,sports WHERE customer.impid=sports.id");
while ($row=mysql_fetch_array($sql))  {
$sport_name=$row['name'];
...
}

Than you write your explode function.

<?php
$to = "vinay.lk99@gmail.com";
$subject = $_POST['PNR'];
$ticket_number = $_POST['tcktnum'];
$PNR = $_POST['PNR'];
$trip_code = $_POST['tcode'];
$date_of_booking = $_POST['dob'];
$date_of_journey = $_POST['doj'];
$place_of_boarding = $_POST['board'];
$boarding_time = $_POST['boarding time'];
$departure_date = $_POST['depdte'];
$departure_time = $_POST['boarding time2'];
$class_of_travel = $_POST['class'];
$arrival_date = $_POST['arvldte'];
$arival_time = $_POST['arival time'];
$tour_program = $_POST['tourprog'];
$amount_advance = $_POST['amountadvance'];
$amount_due = $_POST['amountdue'];
$amount_total = $_POST['amounttotal'];
$name1 = $_POST['name1'];
$sex1 = $_POST['sex1'];
$age1 = $_POST['age1'];
$name2 = $_POST['name2'];
$sex2 = $_POST['sex2'];
$age2 = $_POST['age2'];
$name3 = $_POST['name3'];
$sex3 = $_POST['sex3'];
$age3 = $_POST['age3'];
$name4 = $_POST['name4'];
$sex4 = $_POST['sex4'];
$age4 = $_POST['age4'];
$agency_name = $_POST['agencyname'];
$agency_phone = $_POST['agencyphone'];
$agency_email = $_POST['agencyemail'];
$agency_site = $_POST['agencysite'];
$agency_address = $_POST['agencyaddress'];
$agent_name = $_POST['agent'];
$agent_mob = $_POST['agentmob'];

$body = "JOURNEY DETAILS\n
Ticket Number: $ticket_number\n
PNR: $PNR\n
Trip Code: $trip_code\n
Date of Booking: $date_of_booking\n
Date of Journey: $date_of_journey\n
Place of Boarding: $place_of_boarding\n
Boarding Time: $boarding_time\n
Departure Date: $departure_date\n
Departure Time: $departure_time\n
Class of Travel: $class_of_travel\n
Arrival Date: $arrival_date\n
Arrival Time: $arival_time\n
Tour Program: $tour_program\n\n\n
<h3>RATE DETAILS\n</h3>
Advance: $amount_advance\n
Due: $amount_due\n
Total: $amount_total\n\n\n\n
PASSENGER DETAILS\n
Name-1: $name1\n
Sex-1: $sex1\n
Age-1: $age1\n\n
Name-2: $name2\n
Sex-2: $sex2\n
Age-2: $age2\n
Name-3: $name3\n
Sex-3: $sex3\n
Age-3: $age3\n
Name-4: $name4\n
Sex-4: $sex4\n
Age-4: $age4\n\n\n\n
BOOKING OFFICE DETAILS\n
Agency: $agency_name\n
Phone: $agency_phone\n
e-mail: $agency_email\n
website: $agency_site\n
Address: $agency_address\n
Agent: $agent_name\n
Agent Mobile: $agent_mob\n";

echo "Have a Safe and Happy Journey<br /><br /><br /><a href="" onclick="window.print();return false"><h2>CLICK HERE TO GET THE TICKET PRINTOUT</h2></a>";
mail($to, $subject, $body);
else
echo "kindly try again";
?>

the above script for mail in php is not working can someone help me at the earliest plzzzzzzzzzzzzzzzzzzzz

@lkrvk: is this relate to explode problem above here.

<?php
$to = "vinay.lk99@gmail.com";
$subject = $_POST['PNR'];
$ticket_number = $_POST['tcktnum'];
$PNR = $_POST['PNR'];
$trip_code = $_POST['tcode'];
$date_of_booking = $_POST['dob'];
$date_of_journey = $_POST['doj'];
$place_of_boarding = $_POST['board'];
$boarding_time = $_POST['boarding time'];
$departure_date = $_POST['depdte'];
$departure_time = $_POST['boarding time2'];
$class_of_travel = $_POST['class'];
$arrival_date = $_POST['arvldte'];
$arival_time = $_POST['arival time'];
$tour_program = $_POST['tourprog'];
$amount_advance = $_POST['amountadvance'];
$amount_due = $_POST['amountdue'];
$amount_total = $_POST['amounttotal'];
$name1 = $_POST['name1'];
$sex1 = $_POST['sex1'];
$age1 = $_POST['age1'];
$name2 = $_POST['name2'];
$sex2 = $_POST['sex2'];
$age2 = $_POST['age2'];
$name3 = $_POST['name3'];
$sex3 = $_POST['sex3'];
$age3 = $_POST['age3'];
$name4 = $_POST['name4'];
$sex4 = $_POST['sex4'];
$age4 = $_POST['age4'];
$agency_name = $_POST['agencyname'];
$agency_phone = $_POST['agencyphone'];
$agency_email = $_POST['agencyemail'];
$agency_site = $_POST['agencysite'];
$agency_address = $_POST['agencyaddress'];
$agent_name = $_POST['agent'];
$agent_mob = $_POST['agentmob'];

$body = "JOURNEY DETAILS\n
Ticket Number: $ticket_number\n
PNR: $PNR\n
Trip Code: $trip_code\n
Date of Booking: $date_of_booking\n
Date of Journey: $date_of_journey\n
Place of Boarding: $place_of_boarding\n
Boarding Time: $boarding_time\n
Departure Date: $departure_date\n
Departure Time: $departure_time\n
Class of Travel: $class_of_travel\n
Arrival Date: $arrival_date\n
Arrival Time: $arival_time\n
Tour Program: $tour_program\n\n\n
<h3>RATE DETAILS\n</h3>
Advance: $amount_advance\n
Due: $amount_due\n
Total: $amount_total\n\n\n\n
PASSENGER DETAILS\n
Name-1: $name1\n
Sex-1: $sex1\n
Age-1: $age1\n\n
Name-2: $name2\n
Sex-2: $sex2\n
Age-2: $age2\n
Name-3: $name3\n
Sex-3: $sex3\n
Age-3: $age3\n
Name-4: $name4\n
Sex-4: $sex4\n
Age-4: $age4\n\n\n\n
BOOKING OFFICE DETAILS\n
Agency: $agency_name\n
Phone: $agency_phone\n
e-mail: $agency_email\n
website: $agency_site\n
Address: $agency_address\n
Agent: $agent_name\n
Agent Mobile: $agent_mob\n";
 
echo "Have a Safe and Happy Journey<br /><br /><br /><a href="" onclick="window.print();return false"><h2>CLICK HERE TO GET THE TICKET PRINTOUT</h2></a>";
mail($to, $subject, $body);
else
echo "kindly try again";
?>

Hi,

Thanks, the code is great. But I am trying to query both sport name and price.

$sql1=mysql_query("SELECT * FROM customer,sports WHERE customer.impid=sports.id");

while ($row=mysql_fetch_array($sql))  {
$exp=$sport_id=$row['c_sportsid']; //sports id in customer table as (1,2,3) 
$sport_name=$row['sports_name'];
$sport_price=$row['sports_price'];
$try=explode(",",$exp);

foreach( $try as $name){
echo "$name <br>"; // Id is sequence generate by (1, 2)

// sports id is working  to put the sports name and sports price.

// echo $sport_name, $sport_price
}

First your sql query is not good. You have to have relation between those 2 tables. There are simple example:

$sql1=mysql_query("SELECT * FROM customer,sports WHERE customer.impid=sports.id");
while ($row=mysql_fetch_array($sql))  {
$sport_name=$row['name'];
...
}

I tried the below way, but doesn't work. Can you give support to make this code work

foreach( $try as $name => $price ){
echo "$name, .$.$price  <br>";
}

Than you write your explode function.

Hi,

Thanks, the code is great. But I am trying to query both sport name and price.

$sql1=mysql_query("SELECT * FROM customer,sports WHERE customer.impid=sports.id");

while ($row=mysql_fetch_array($sql))  {
$exp=$sport_id=$row['c_sportsid']; //sports id in customer table as (1,2,3) 
$sport_name=$row['sports_name'];
$sport_price=$row['sports_price'];
$try=explode(",",$exp);

foreach( $try as $name){
echo "$name <br>"; // Id is sequence generate by (1, 2)

// sports id is working  to put the sports name and sports price.

// echo $sport_name, $sport_price
}

And where is price? In which table? Write your database tables...

There only two tables, sports table and customer table.

Actually customer will input the sports to the c_sportsid (1,2) and After that they will find which are the sports name and prices from sports table. Customer table and sports table is relate through ID. eg: c_sportsid is taken from sport_id using implode function.

// customer table
c_id, c_sportsid, //fields  
1, 1,2
2, 2

// sports table
sport_id, sport_name, sport_price
1, football, 100 
2, basketball, 200

Hi,

$sql1=mysql_query("SELECT * FROM customer,sports WHERE customer.impid=sports.id");

while ($row=mysql_fetch_array($sql))  {
$exp=$sport_id=$row['c_sportsid']; //sports id in customer table as (1,2,3) 
$sport_name=$row['sports_name'];
[B]$sport_price=$row['sports_price'];[/B]

This should work. Mysql is now ok. Only if there is problem in php

Thanks for everyone, at last it works.

$sql4=mysql_query("SELECT * FROM customer WHERE c_id=1") or die (mysql_error());

// while loop to explode 
while ($row4=mysql_fetch_array($sql4))  {
$exp=$act_name=$row4['impid'];
$aname=$row4['sport_name'];
$price=$row4['sport_price'];
$try=explode(",",$exp);
}

if(strstr($try, ',')){
$array=explode(",",$try);
}else{

$array = array($try);
}

// For each to link 
foreach( $try as $name){
 $sql2 = mysql_query("SELECT * FROM sports where sport_id='".$name."'") or die (mysql_error());
 $result2 = mysql_fetch_array($sql2);
 echo $result2['sport_name']; 
 echo " $". $id[]=$result2['sport_price'] ."<br>"; 
}
echo $total = array_sum($id);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.